android 监听安装来源_Android编程监听APK安装与删除等过程的方法
本文實例講述了Android編程監聽APK安裝與刪除等過程的方法。分享給大家供大家參考,具體如下:
軟件下載后的一系列動作監聽:先前是通過Service監聽掃描獲取狀態,以后用這個方法測試使用
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class getBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
Toast.makeText(context, "有應用被添加", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
Toast.makeText(context, "有應用被改變", Toast.LENGTH_LONG).show();
}*/
else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
Toast.makeText(context, "有應用被重啟", Toast.LENGTH_LONG).show();
}*/
/* else if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
Toast.makeText(context, "有應用被安裝", Toast.LENGTH_LONG).show();
}*/
}
}
package="zy.Broadcast"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">
代碼實現添加:
private final BroadcastReceiver apkInstallListener = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
System.out.println("**************Broadcase*************");
File file = uninstallApk.get(isDeleted);
System.out.println(file.toString()+"*****");
file.delete();
//System.out.println(uninstallApk.size()+"(*******"+uApks.size());
if(uninstallApk!=null&&uApks!=null)
{
uninstallApk.remove(isDeleted);
uApks.remove(isDeleted);
}
//清除集合里面的值
if(uninstallApk!=null)
{
System.out.println("onpause******"+uninstallApk.size());
uninstallApk.clear();
}
if(uApks!=null)
{
uApks.clear();
}
System.out.println("******應用添加***"+isDeleted);
Toast.makeText(context, "有應用被添加"+isDeleted, Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
System.out.println("*****應用被刪除");
Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
Toast.makeText(context, "有應用被改變", Toast.LENGTH_LONG).show();
}*/
else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
System.out.println("****應用被替換");
Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
Toast.makeText(context, "有應用被重啟", Toast.LENGTH_LONG).show();
}*/
/* else if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
Toast.makeText(context, "有應用被安裝", Toast.LENGTH_LONG).show();
}*/
}
};
// 注冊監聽
private void registerSDCardListener(){
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addDataScheme("package");
registerReceiver(apkInstallListener, intentFilter);
}
java里的調用 registerSDCardListener()
@Override
protected void onDestroy()
{
super.onDestroy();
//unregisterReceiver(apkInstallListener);
}
希望本文所述對大家Android程序設計有所幫助。
總結
以上是生活随笔為你收集整理的android 监听安装来源_Android编程监听APK安装与删除等过程的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 禁用计算机组策略和管理,计算机正在使用时
- 下一篇: 单分支 两路分支和多分支的if结构_Ja