别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述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 {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"); 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 中使用自定义权限】
推荐阅读
- IP地址突变之谜
- yosemite开启HAXM硬件加速执行安卓虚拟机
- angular 4 实战开发--安卓兼容问题
- Android面试准备 第二天 第五例 数据存储
- Android零基础入门第12节(熟悉Android Studio界面,开始装逼卖萌)
- 使用Android注解来改善代码
- Android 学习笔记--9妹图片+代码混淆+打包
- 网狐荣耀版安卓端的签名与打包
- Android 组件之Service解析