首页 > 代码库 > android 中使用自定义权限在广播中的利用
android 中使用自定义权限在广播中的利用
1、在一个进程中发送一个有自定义权限的广播,另外一个进程中拥有广播接受者接受到该广播
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="im.weiyuan.com.hkkj"> <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"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.weiyuan.test"></action> </intent-filter> </receiver> </application> </manifest>
package im.weiyuan.com.hkkj; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving Toast.makeText(context,"收到了广播",Toast.LENGTH_LONG).show(); } }
在另外一个进程中只需要使用下面的代码,就可以发送一个广播
package im.weiyuan.com.viewutils; import android.content.Intent; import android.os.PersistableBundle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button btn_main; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(savedInstanceState != null){ Toast.makeText(MainActivity.this,savedInstanceState.getString("data"),Toast.LENGTH_LONG).show(); } btn_main = (Button) findViewById(R.id.btn_main); btn_main.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("com.weiyuan.test"); sendBroadcast(intent); } }); } @Override protected void onStop() { super.onStop(); Log.d("123456","onStop is called"); } @Override protected void onPause() { super.onPause(); Log.d("123456","onPause is called"); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.d("123456","onSaveInstanceState is called"); outState.putString("data","1020223"); } }
现在我们在发送广播的时候带上一个自定义权限:
package im.weiyuan.com.viewutils; import android.content.Intent; import android.os.PersistableBundle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button btn_main; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(savedInstanceState != null){ Toast.makeText(MainActivity.this,savedInstanceState.getString("data"),Toast.LENGTH_LONG).show(); } btn_main = (Button) findViewById(R.id.btn_main); btn_main.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("com.weiyuan.test"); String pression = "com.weiyuan.presstion"; sendBroadcast(intent,pression); } }); } @Override protected void onStop() { super.onStop(); Log.d("123456","onStop is called"); } @Override protected void onPause() { super.onPause(); Log.d("123456","onPause is called"); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.d("123456","onSaveInstanceState is called"); outState.putString("data","1020223"); } }
我们在发送广播的时候带上了一个权限:
String pression = "com.weiyuan.presstion";
在另外一个进程的广播接受者需要对该权限在清单文件中进行声明和注册
否则广播接受者无法接受到发送带有权限的广播
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="im.weiyuan.com.hkkj"> <permission android:name="com.weiyuan.presstion"></permission> <uses-permission android:name="com.weiyuan.presstion"></uses-permission> <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"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.weiyuan.test"></action> </intent-filter> </receiver> </application> </manifest>
带有自定义权限关闭的使用场景:
手机上装有上面的几个app,当淘宝启动的时候,发送带有权限的广播,支持包 uc注册带有权限的广播接受者,当淘宝启动的时候,uc等app收到广播之后,就会自己启动起来,达到自启动的功能。
android 中使用自定义权限在广播中的利用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。