BroadcastReceiver几种常见监听详解手机开发

1.BroadcastReceiver监听拨号

<intent-filter android:priority="1000" > 
             <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
</intent-filter> 

 

@Override 
    public void onReceive(Context context, Intent intent) { 
        //获取拨打电话的号码 
        String call=getResultData(); 
        //在电话号码前加上110,然后返回数据 
        setResultData("110"+call); 
    } 

 

2.BroadcastReceiver监听短信

<receiver android:name="SmsReceiver"> 
         <intent-filter android:priority="1000"> 
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> 
          </intent-filter> 
</receiver>

 

3.BroadcastReceiver监听SD卡状态

<receiver Android:name=".SDStatusReceiver"> 
     <intent-filter > 
         <action android:name="android.intent.action.MEDIA_MOUNTED"/> 
         <action android:name="android.intent.action.MEDIA_REMOVED"/> 
         <action android:name="android.intent.action.MEDIA_UNMOUNTED"/> 
         <data android:scheme="file"/> 
     </intent-filter> 
</receiver 

  

public class SDStatusReceiver extends BroadcastReceiver {   
   
    @Override   
    public void onReceive(Context context, Intent intent) {   
        //判断收到的到底是什么广播   
        String action = intent.getAction();   
        if("android.intent.action.MEDIA_MOUNTED".equals(action)){   
            Toast.makeText(context, "SD卡可用", 0).show();   
        }   
        else if("android.intent.action.MEDIA_REMOVED".equals(action)){   
            Toast.makeText(context, "SD卡拔出", 0).show();   
        }   
        else if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){   
            Toast.makeText(context, "SD卡不可用", 0).show();   
        }   
    }   
}   

  

4.BroadcastReceiver监听开机

<receiver android:name="BootCompeletedReceiver"> 
            <intent-filter > 
                <action android:name="android.intent.action.BOOT_COMPLETED"/> 
            </intent-filter> 
</receiver>

 

5.BroadcastReceiver监听应用安装卸载

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

  

public class IntallReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { String packageName = intent.getData().toString(); String action = intent.getAction(); // 如果是卸载 if ("android.intent.action.PACKAGE_REMOVED".equals(action)) { Toast.makeText(context, packageName+"应用程序被卸载", 1).show(); System.out.println(packageName+"已删除"); } else if ("android.intent.action.PACKAGE_ADDED".equals(action)) { Toast.makeText(context, packageName+"应用程序安装", 1).show(); System.out.println(packageName + "已安装"); } } }

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/3085.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论