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收到广播之后,就会自己启动起来,达到自启动的功能。



posted on 2017-07-23 11:27  luzhouxiaoshuai  阅读(1101)  评论(0编辑  收藏  举报

导航