博观而约取,厚积而薄发。这篇文章主要讲述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> < receiverandroid: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 {privateButton 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"); }}
【android 中使用自定义权限在广播中的利用】现在我们在发送广播的时候带上一个自定义权限:
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 {privateButton 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> < receiverandroid: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 中IntentService的使用场景
- Windows App开发之经常使用控件与应用栏
- TCP连接创建及结束过程详解
- IPv6:推进NGI应用的新空间
- 万兆多模与DMD测试
- 应用于IEEE 802.3ae 10Gbit/s以太网的多模光纤
- 下 NGN问题理解及其进展策略思考
- Skype收购语音处理技术公司Sonorit
- 企业局域网本地路由设置5办法