码迷,mamicode.com
首页 > 移动开发 > 详细

app状态监听广播

时间:2016-07-12 10:28:05      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

手机中的应用在安装、更新和卸载时都会发送广播

清单文件


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xidian.dy.com.chujia">
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="主界面">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".SDstatusReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED"/>
                <action android:name="android.intent.action.PACKAGE_CHANGED"/>
                <action android:name="android.intent.action.PACKAGE_REMOVED"/>
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

在清单文件中设置要接收的广播

广播接收者


package xidian.dy.com.chujia;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

/**
 * Created by dy on 2016/7/12.
 */
public class APPstatusReceiver 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_LONG).show();
        }
        else if("android.intent.action.PACKAGE_CHANGED".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();
        }
    }
}

 

app状态监听广播

标签:

原文地址:http://www.cnblogs.com/xidongyu/p/5662452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!