一、自定义权限在Activity中的使用
首先在一个应用项目PermissionDemo2中创建一个Activity,名为SecondActivity,并将该SecondActivity注册到AndroidManifest.xml中,并在AndroidManifest.xml文件中对该SecondActivity设置intent-filter属性,以及permission属性。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.permissiondemo2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PermissionDemo2">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:permission="com.test.permission.second">
<intent-filter>
<action android:name="com.test.permissiondemo2.SecondActivity"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
配置项如下:
<activity android:name=".SecondActivity"
android:permission="com.test.permission.second">
<intent-filter>
<action android:name="com.test.permissiondemo2.SecondActivity"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
SecondActivity.java
package com.test.permissiondemo2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class SecondActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("我是SecondActivity");
textView.setTextSize(30);
setContentView(textView);
}
}
在另外一个应用项目PermissionDemo1中创建一个Activity,在该activity中添加一个button控件,并编写相关的点击事件代码。
MainActivity.java
package com.test.permissiondemo1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view) {
Intent intent = new Intent();
//该action中的内容为另外一个带权限的Activity中设置的action name,再AndroidManifest.xml中有配置
intent.setAction("com.test.permissiondemo2.SecondActivity");
startActivity(intent);
}
}
在PermissionDemo1中的AndroidManifest.xml配置文件中添加PermissionDemo2中SecondActivity的权限声明和使用:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.permissiondemo1">
<!--声明权限-->
<permission android:name="com.test.permission.second"/>
<!--使用权限-->
<uses-permission android:name="com.test.permission.second" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PermissionDemo1">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
声明权限和使用权限
<!--声明权限-->
<permission android:name="com.test.permission.second"/>
<!--使用权限-->
<uses-permission android:name="com.test.permission.second" />
二、自定义权限在广播中的使用
首先一个应用项目PermissionBroastCastDemo1中创建一个Activity,在该activity中添加一个button控件,并编写相关的点击事件代码。
布局文件: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送带权限的广播"
android:onClick="send" />
</LinearLayout>
MainActivity.java
package com.test.permissionbroadcastdemo1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view) {
Intent intent = new Intent();
intent.setAction("com.test.permissionbroadcast.mybroadcast");
//sendBroadcast(intent); //发送没有带权限的广播
String receiverPermission = "com.test.permission.mybroadcast"; //声明的权限
sendBroadcast(intent, receiverPermission); //发送带权限的广播
}
}
在另外一个应用项目PermissionBroastCastDemo2中创建一个MyBroadCast,继承BroadcastReceiver,并将该MyBroadCast注册到AndroidManifest.xml中,并在AndroidManifest.xml文件中对该SecondActivity设置intent-filter属性。
MyBroadCast.java
package com.test.permissionbroadcastdemo2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyBroadCast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("abc", "接收到广播: " + intent);
Toast.makeText(context, "接收到广播: " + intent, Toast.LENGTH_SHORT).show();
}
}
配置文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.permissionbroadcastdemo2">
<!--声明权限-->
<permission android:name="com.test.permission.mybroadcast"/>
<!--使用权限-->
<uses-permission android:name="com.test.permission.mybroadcast"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PermissionBroadcastDemo2">
<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=".MyBroadCast">
<intent-filter>
<action android:name="com.test.permissionbroadcast.mybroadcast"/>
</intent-filter>
</receiver>
</application>
</manifest>
这里特别注意,receiver中配置的action name必须跟应用项目PermissionBroadcastDemo1中配置的action一致,并且必须声明和使用应用项目PermissionBroadcastDemo1中设置的权限
如下为应用项目PermissionBroadcastDemo1中配置的action和权限。
intent.setAction("com.test.permissionbroadcast.mybroadcast");
String receiverPermission = "com.test.permission.mybroadcast"; //声明的权限