|
前一个问题是论坛里的一位朋友提出来的:“如何在应用安装到手机里时,自动在桌面增加快捷方式?” ,第二个问题是在网上看到的:“apk安装后如何自启动” 。
很显然,除非在应用安装后有相关的广播能被捕获到,否则就没法做了,事实是有的:Intent.ACTION_PACKAGE_ADDED。
Launcher中的应用列表正是这么做的:
<Launcher.java>
Java代码
/**
* Registers various intent receivers. The current implementation registers
* only a wallpaper intent receiver to let other applications change the
* wallpaper.
*/
private void registerIntentReceivers() {
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKjAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
registerReceiver(mApplicationsReceiver, filter);
filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(mCloseSystemDialogsReceiver, filter);
}
复制代码
我依样画葫芦尝试了一下,直接在我的应用中实现BroadcastReceiver并在XML注册,对action=Intent.ACTION_PACKAGE_ADDED进行捕获,但是没捕获到。
后来一查文档才发现这是行不通的:
public static final [url=]String[/url] ACTION_PACKAGE_ADDED
Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast.
看来在应用自身中通过BroadcastReceiver来捕获Add消息是不行的,但是我想到了另一种折中的实现方法——通过另一个应用来辅助实现。
需要先实现一个包含对ntent.ACTION_PACKAGE_ADDED进行捕获的BroadcastReceiver的应用,首先安装到手机上,在他接受到消息后再向你的应用返回一个广播。你需要在你的应用中实现实现对应的BroadcastReceiver。
具体实现:
<辅助apk>
Java代码
public class PackageChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
final String packageName = intent.getData().getSchemeSpecificPart();
final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
// 通知对应的应用
Intent notifyIntent = new Intent("com.app.action.notifier");
notifyIntent.setPackage(packageName);
notifyIntent.putExtra("action", action);
notifyIntent.putExtra("replace", replacing);
context.sendBroadcast(notifyIntent);
}
}
复制代码
Java代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.notifier"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="ackageChangeReceiver">
<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>
<data android:scheme="package"></data>
</intent-filter>
</receiver>
</application>
</manifest>
复制代码<你的应用>
Java代码
public class PackageChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getStringExtra("action");
boolean replace = intent.getBooleanExtra("replace", false);
if(action.equals(Intent.ACTION_PACKAGE_ADDED)){
// do some thing you want.
}
}
}
复制代码
xml中注册广播
Java代码
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="com.app.action.notifier"></action>
</intent-filter>
</receiver>
复制代码
优缺点
缺点:必须先安装辅助apk
优点:仅需一次安装,之后使用只需在应用中实现并在XML中注册BroadcastReceiver就能捕获到安装事件,从而执行相应的操作(添加桌面图标,自启动...)
------------------------------------------------------------------------------------------------
可能说得有点乱,把代码也传上来好了... 大家还有什么好的思路可以讨论下哈~~
AppChangeNotifier.rar (12.6 KB, 下载次数: 0) |
|