Android实战教程第六篇之一键锁屏应用问题解决

所属分类: 软件编程 / Android 阅读数: 40
收藏 0 赞 0 分享

上一篇,初步开发了这个应用,功能都有了(见https://www.jb51.net/article/96992.htm 点击打开链接)。

但是遗留了两个问题:1、还是无法卸载;2、必须手动去点击应用程序进入程序,再点击按钮,这显得很麻烦。

这一篇就解决上面两个问题,做出最好的效果。

首先解决无法卸载问题:

在清单文件中,再配置一键卸载的主活动(两个主活动无所谓,只不过在桌面上显示两个图标而已,其实仍然是一个应用)

<activity 
   android:name="com.itydl.lockscreen.Remove" 
   android:label="一键卸载" > 
   <intent-filter> 
    <action android:name="android.intent.action.MAIN" /> 
 
    <category android:name="android.intent.category.LAUNCHER" /> 
   </intent-filter> 
  </activity> 

在新建的活动里面,写取消激活和卸载的代码:

package com.itydl.lockscreen; 
 
import android.app.Activity; 
import android.app.admin.DevicePolicyManager; 
import android.content.ComponentName; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
 
public class Remove extends Activity { 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  // 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_remove); 
 } 
  
 public void remove(View v){ 
  //调用卸载的界面,要去上层源码中看一下卸载界面的意图是什么。通过查看源码,如下: 
  /** 
   * <intent-filter> 
    <action android:name="android.intent.action.VIEW" /> 
    <action android:name="android.intent.action.DELETE" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <data android:scheme="package" /> 
   </intent-filter> 
   */ 
   
  //在卸载之前首先要取消设激活设备管理器 
  //获取设备管理器 
  DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); 
  ComponentName who = new ComponentName(this,DeviceAdminSample.class); 
  dpm.removeActiveAdmin(who);//Remove a current administration component. This can only be called by the application that owns the administration component 
   
  //卸载,启动卸载界面 
  Intent remove = new Intent("android.intent.action.DELETE"); 
  remove.addCategory("android.intent.category.DEFAULT"); 
  remove.setData(Uri.parse("package:"+getPackageName()));//表示卸载本程序,Return the name of this application's package. 
 
  startActivity(remove);//卸载用户apk界面 
 } 
} 

但是要注意一点,新建的一键卸载布局,一定做以下修改:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" > --------------------这里一定修改。 
 
 <Button 
  android:onClick="remove" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="一键卸载" /> 
 
</RelativeLayout> 

运行程序,当用户想要卸载的时候,点击一键卸载进入卸载界面,即可对这个应用完成卸载了。
最后在用户体验上做点文章:
这个时候,一键锁屏是通过点击按钮实现的,为了省事,不要开启锁屏的界面。
把锁屏界面按钮全部删掉,背景设置为透明色

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#00000000"> 
 ---------------------设置为透明色 
  
 
</RelativeLayout> 

在锁屏活动的onCreat方法里面加入一句代码:lockScreen(null);表示不需要任何view去启动点击事件。
这样打开应用的时候就调用lockScreen(View v)里面的锁屏或者开启设备管理器功能,代码如下:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 dmp = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); 
 lockScreen(null); 
} 

同时,一键卸载的应用也做以上同样的事情:

重新运行程序,一运行就进入激活设备管理器界面。现在把一键锁屏图标放置到主界面,一点击就锁屏,特别的方便。而且想卸载这个程序的时候,一点击就直接卸载掉了。

到现在为止,这个应用,完美开发完毕,秒杀市面上99%的一键锁屏应用。

下面是应用的截图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多