Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

android - How to correctly filter Package replaced broadcast

I am trying to catch the package replaced broadcast for my app and only my app, but for some reason in my reciever I am the broadcast for every app that is updated. I thought you only needed to set the intent filter in the manifest file to your app, but maybe I am wrong?

Here's my code(manifest):

        <receiver android:name=".UpdateReciever">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" android:path="com.my.app" />
        </intent-filter>
    </receiver>

Reciever:

public class AppUpdateReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context con, Intent intent) {

        //code..    
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add this to your onReceive method:

if (intent.getDataString().contains("com.my.app")){
    ...
}

EDIT: Note that registering for ACTION_PACKAGE_REPLACED causes your app to be started up every time any app is updated, if it wasn't already open. I don't know how to avoid this before API 12, but in API 12 you can register for ACTION_MY_PACKAGE_REPLACED so you don't have to filter the intent and your app won't be started unnecessarily by other apps being updated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...