美文网首页
监听应用状态

监听应用状态

作者: yanghanbin_it | 来源:发表于2017-06-08 14:59 被阅读0次

    监听应用安装,升级,卸载的广播

    public class AppStateReceiver extends BroadcastReceiver {
    
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            String action = intent.getAction();
    
            Uri uri = intent.getData();
    
            if ("android.intent.action.PACKAGE_ADDED".equals(action)) {
                Toast.makeText(context, "有应用被安装:" + uri.toString(), Toast.LENGTH_SHORT).show();
            } else if ("android.intent.action.PACKAGE_REPLACED".equals(action)) {
                Toast.makeText(context, "有应用被升级" + uri.toString(), Toast.LENGTH_SHORT).show();
            } else if ("android.intent.action.PACKAGE_REMOVED".equals(action)) {
                Toast.makeText(context, "有应用被卸载" + uri.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }
    
    <receiver
        android:name=".AppStateReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED"></action>
            <action android:name="android.intent.action.PACKAGE_REPLACED"></action>
            <action android:name="android.intent.action.PACKAGE_REMOVED"></action>
    
            <data android:scheme="package"></data>
        </intent-filter>
    </receiver>
    

    相关文章

      网友评论

          本文标题:监听应用状态

          本文链接:https://www.haomeiwen.com/subject/trftqxtx.html