Mis*_*Dev 22 android broadcast
我的应用程序不在Play商店上验证在网络上如果有新版本并下载并启动它.安装完毕后,我想重新启动应用程序,我会用一个BroadcastRecevier带ACTION_PACKAGE_REPLACED.这是代码:
广播:
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
ApplicationInfo app = new ApplicationInfo();
if(app.packageName.equals("it.android.downloadapk")){
Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(app.packageName);
context.startActivity(LaunchIntent);
}
}
}
Run Code Online (Sandbox Code Playgroud)
表现:
<receiver android:name="it.android.downloadapk.Broadcast">
<intent-filter>
<action android:name="android.intent.action.ACTION_PACKAGE_REPLACED"></action>
<data android:scheme="package" android:path="it.android.downloadapk" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
问题是,当我安装新的apk时,广播似乎没有启动,为什么?
and*_*per 28
看到这个:
正确的解决方法是在清单中使用错误的字符串:http: //developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED
它应该是"android.intent.action.PACKAGE_REPLACED"而不是.
好吧,我看到我所写的内容仍然不足以试一试,所以我会做一个例外并发布一个完整的项目只是为了表明它的工作原理:app代码在一个名为"com.broadcast_receiver_test"的包中.不要忘记在测试之前运行它,否则它将无法在某些Android版本上运行(我认为API 11+).
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast_receiver_test" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:name=".BroadcastReceiverTestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
MyBroadcastReceiver.java:
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(final Context context,final Intent intent)
{
final String msg="intent:"+intent+" action:"+intent.getAction();
Log.d("DEBUG",msg);
Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
请运行它,看看它是否完美.
编辑:如果您的应用程序适用于API12及更高版本,并且只希望处理更新您的应用程序的情况,您可以单独使用此意图:
http://developer.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED
我将以下接收器放在AndroidManifest.xml中
<receiver android:name=".StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
因此,我的应用程序可以在更新以及设备重新启动时启动。当然,正如所有人都提到的那样,您需要MY_PACKAGE_REPLACED使用API 12+。
| 归档时间: |
|
| 查看次数: |
24601 次 |
| 最近记录: |