Android中如何收听特定应用安装成功的广播

一、manifest的配置

        <receiver android:name=".AppReceiver">
           <intent-filter>
              <action android:name="android.intent.action.PACKAGE_ADDED" />
              <data android:scheme="package" />
              </intent-filter>
        </receiver>    

 

二、receiver中的判断

复制代码
public class AppReceiver extends BroadcastReceiver
{
    private static final int PACKAGE_NAME_START_INDEX = 8;
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent == null)
        {
            return;
        }
        
        if(intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED))
        {
            String data = intent.getDataString();
            
            if(data == null || data.length() <= PACKAGE_NAME_START_INDEX)
            {
                return;
            }
            
            String packageName = data.substring(PACKAGE_NAME_START_INDEX);
            
            if(packageName.equals(AppActivity.PACKAGE_NAME) )
            {
            //todo
            }
        }
        
    }

}
复制代码

 

你可能感兴趣的:(Android中如何收听特定应用安装成功的广播)