Android中Intent
1)利用Inent监听应用程序的安装、替换、删除等
与包相关的Intent:
Intent.ACTION_PACKAGE_INSTALL
Intent.ACTION_PACKAGE_ADDED
Intent.ACTION_PACKAGE_REPLACED
Intent.ACTION_PACKAGE_REMOVED
Intent.ACTION_PACKAGE_CHANGED
Intent.ACTION_PACKAGE_RESTARTED
Intent.ACTION_PACKAGE_DATA_CLEARED
首先定义一个Broadcast类——HandlePackageAction,代码如下:
- public class HandlePackageAction extends BroadcastReceiver {
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (Intent.ACTION_PACKAGE_ADDED.equals(action))
- {
- Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();
- }
- else if (Intent.ACTION_PACKAGE_REMOVED.equals(action))
- {
- Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();
- }
- else if (Intent.ACTION_PACKAGE_CHANGED.equals(action))
- {
- Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show();
- }
- else if (Intent.ACTION_PACKAGE_REPLACED.equals(action))
- {
- Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
- }
- else if (Intent.ACTION_PACKAGE_RESTARTED.equals(action))
- {
- Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show();
- }
- else if (Intent.ACTION_PACKAGE_INSTALL.equals(action))
- {
- Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show();
- }
- }
- }
然后在AndroidManifest.xml中注册HandlePackageAction,内容如下:
- <receiver android:name=".HandlePackageAction"
- android:enabled="true">
- <intent-filter>
- <action android:name="android.intent.action.PACKAGE_ADDED"></action>
- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>
- <action android:name="android.intent.action.PACKAGE_REMOVED"></action>
- <action android:name="android.intent.action.PACKAGE_REPLACED"></action>
- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>
- <action android:name="android.intent.action.PACKAGE_INSTALL"></action>
- <data android:scheme="package"></data>
- </intent-filter>
- </receiver>
2)安装、卸载应用程序
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(new File("/sdcard/Test.apk")),
- "application/vnd.android.package-archive");
- startActivity(intent); // 安装 程序
- Uri packageURI = Uri.parse("package:com.ygc.Test");
- Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
- startActivity(uninstallIntent);// 正常卸载程序
3)开机启动Activity
首先定义一个Broadcast类——HandleBootComplete,代码如下:
- public class HandleBootComplete extends BroadcastReceiver {
- public void onReceive(Context context, Intent intent) {
- Intent startActivityIntent = new Intent(context, DrawText.class);
- startActivityIntent.setAction(Intent.ACTION_MAIN);
- startActivityIntent.addCategory(Intent.CATEGORY_LAUNCHER);
- startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(startActivityIntent);
- }
- }
然后在AndroidManifest.xml中注册HandleBootComplete,内容如下:
- <!-- 添加权限 -->
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
- <receiver android:name="HandleBootComplete" android:enabled="true">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- <category android:name="android.intent.category.HOME" />
- </intent-filter>
- </receiver>