android 中使用自定义权限
Posted luzhouxiaoshuai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 中使用自定义权限相关的知识,希望对你有一定的参考价值。
1、如果在一个进程中启动另外一个进程的activity
<?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> <activity android:name=".Main2Activity"> <intent-filter> <action android:name="com.weiyuan.test"></action> <category android:name="android.intent.category.DEFAULT"></category> </intent-filter> </activity> </application> </manifest>
package im.weiyuan.com.hkkj;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toast.makeText(Main2Activity.this,"jksajkdjk",Toast.LENGTH_LONG).show();
}
}
现在我们在另外一个进程之中启动商密的Main2Activity
只需要下面的代码:
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"); startActivity(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"); } }
这里就可以启动了。
现在我们在Main2Activity中添加自定义权限
<?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> <activity android:name=".Main2Activity" android:permission="com.weiyuan.sb" > <intent-filter> <action android:name="com.weiyuan.test"></action> <category android:name="android.intent.category.DEFAULT"></category> </intent-filter> </activity> </application> </manifest>
启动端如果不进行配置,就会奔溃,启动者的
MainActivity 清单文件需要对上面的权限进行配置
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="im.weiyuan.com.viewutils"> <permission android:name="com.weiyuan.sb"></permission> <uses-permission android:name="com.weiyuan.sb"></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> </application> </manifest>
然后在使用下面的代码进行启动:
Intent intent = new Intent();
intent.setAction("com.weiyuan.test");
startActivity(intent);
以上是关于android 中使用自定义权限的主要内容,如果未能解决你的问题,请参考以下文章