Ton*_*Ton 27 android broadcastreceiver
当我的应用程序更新或树脂安装时,我正在使用ACTION_MY_PACKAGE_REPLACED来接收.我的问题是从未触发事件(我尝试过Eclipse和真实设备).这就是我做的:
表现:
<receiver android:name=".MyEventReceiver" >
<intent-filter android:priority="1000" >
<action android:name="android.intent.action.ACTION_MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
码:
public class MyEventReceiver extends BroadcastReceiver
{
@Override public void onReceive(Context context, Intent intent)
{
if ("android.intent.action.ACTION_MY_PACKAGE_REPLACED".equals(intent.getAction()))
{ //Restart services
}
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码很简单,实际上我使用了其他事件,比如BOOT_COMPLETED和其他事件,它们可以工作但ACTION_MY_PACKAGE_REPLACED.谢谢.
and*_*per 23
出于某种原因,谷歌开发人员(名为"Dianne Hackborn")认为可以单独注册当前应用程序的PACKAGE_REPLACED意图(链接在这里).
但是,我找不到任何正确的方法,所以我做了一个妥协:它将使用最新的API.
遗憾的是,我无法找出无法调试的原因,但它确实有效(如果您愿意,可以写入日志).
这是代码:
表现:
<receiver
android:name=".OnUpgradeBroadcastReceiver"
android:enabled="@bool/is_at_most_api_11" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<receiver
android:name=".OnUpgradeBroadcastReceiver"
android:enabled="@bool/is_at_least_api_12" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
RES /值/ version_checks.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="is_at_least_api_12" type="bool">false</item>
<item name="is_at_most_api_11" type="bool">true</item>
</resources>
Run Code Online (Sandbox Code Playgroud)
RES /值-V12/version_checks.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="is_at_least_api_12" type="bool">true</item>
<item name="is_at_most_api_11" type="bool">false</item>
</resources>
Run Code Online (Sandbox Code Playgroud)
OnUpgradeBroadcastReceiver.java
public class OnUpgradeBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if (VERSION.SDK_INT <= VERSION_CODES.HONEYCOMB
&& !context.getPackageName().equals(intent.getData().getSchemeSpecificPart())) {
android.util.Log.d("AppLog", "other apps were upgraded");
return;
}
android.util.Log.d("AppLog", "current app was upgraded");
Run Code Online (Sandbox Code Playgroud)
cpr*_*ack 14
该接受的答案不工作了与Android工作室1.0+因为清单合并的问题,因为看到这里.完全基于android开发人员的回答,我用以下实现修复了这个问题:
AndroidManifest.xml中:
<receiver android:name=".UpdateReceiver$LegacyUpdateReceiver" android:enabled="@bool/shouldUseActionPackageReplaced" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<receiver android:name=".UpdateReceiver" android:enabled="@bool/shouldUseActionMyPackageReplaced" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
/res/values/resources.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="shouldUseActionPackageReplaced">true</bool>
<bool name="shouldUseActionMyPackageReplaced">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)
/res/values-v12/resources.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="shouldUseActionPackageReplaced">false</bool>
<bool name="shouldUseActionMyPackageReplaced">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)
UpdateReceiver.java:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class UpdateReceiver extends BroadcastReceiver
{
public static class LegacyUpdateReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent != null && intent.getData() != null && context.getPackageName().equals(intent.getData().getSchemeSpecificPart()))
{
onUpdate(context);
}
}
}
@Override
public void onReceive(Context context, Intent intent)
{
onUpdate(context);
}
public static void onUpdate(Context context)
{
Log.d("LOG", "Current app updated");
}
}
Run Code Online (Sandbox Code Playgroud)
从所有用户那里获取信息我可以用这种方式解决我的情况.所有这些都是正确的,没有什么要注意的事项:
在清单中:
<receiver android:name=".MyEventReceiver" >
<intent-filter android:priority="1000" >
<!--other actions I need-->
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
和代码:
public class MyEventReceiver extends BroadcastReceiver
{
@Override public void onReceive(Context context, Intent intent)
{
if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction()))
{ if(intent.getData().getSchemeSpecificPart().equals(context.getPackageName()))
{ //Restart services.
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的Android版本(2.3 Gingerbread)中,我无法使用MY_PACKAGE_REPLACED,但是我们使用PACKAGE_REPLACED解决了(会建议更换任何应用),但询问是否是我们的:
if(intent.getData().getSchemeSpecificPart().equals(context.getPackageName()))
{
}
Run Code Online (Sandbox Code Playgroud)
谢谢大家
我只想在这里添加我自己的两便士,因为我有问题让它工作并调试它..我正在使用Lollipop设备:
这是我使用的代码:
<receiver android:name=".services.OnUpdateReceiver">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
要调试它,您需要确保已在手机上安装了更新,只需通过Eclipse调试就可以了,并且至少打开了一次应用程序,然后您可以简单地编辑调试配置:
eclipse > run > debug configurations > launch action (do nothing) > then F11 like normal
Run Code Online (Sandbox Code Playgroud)
我确认它是通过写一个小文件到SD卡
| 归档时间: |
|
| 查看次数: |
27447 次 |
| 最近记录: |