Use custom Permissions in Android

Source: Internet
Author: User

1. If the activity of another process is initiated in one process

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Im.weiyuan.com.hkkj">    <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme">        <ActivityAndroid:name=". Mainactivity ">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <ActivityAndroid:name=". Main2activity ">            <Intent-filter>                <ActionAndroid:name= "Com.weiyuan.test"></Action>                <categoryAndroid: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 ();    }}

Now we're starting the main2activity in another process.

Only the following code is required:

 Packageim.weiyuan.com.viewutils;Importandroid.content.Intent;ImportAndroid.os.PersistableBundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {PrivateButton Btn_main; @Overrideprotected voidonCreate (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 (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setaction ("Com.weiyuan.test");            StartActivity (Intent);    }        }); } @Overrideprotected voidOnStop () {Super. OnStop (); LOG.D ("123456", "OnStop is called"); } @Overrideprotected voidOnPause () {Super. OnPause (); LOG.D ("123456", "OnPause is called"); } @Override Public voidonsaveinstancestate (Bundle outstate) {Super. Onsaveinstancestate (outstate); LOG.D ("123456", "Onsaveinstancestate is called"); Outstate.putstring ("Data", "1020223"); }}

Here it's ready to start.

Now we add custom permissions in Main2activity

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Im.weiyuan.com.hkkj">    <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme">        <ActivityAndroid:name=". Mainactivity ">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <ActivityAndroid:name=". Main2activity " android:permission= "COM.WEIYUAN.SB"             >            <Intent-filter>                <ActionAndroid:name= "Com.weiyuan.test"></Action>                <categoryAndroid:name= "Android.intent.category.DEFAULT"></category>            </Intent-filter>        </Activity>    </Application></Manifest>

Start-up if not configured, it will crash, the initiator's

The mainactivity manifest file needs to be configured with the above permissions
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns: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>         <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme">        <ActivityAndroid:name=". Mainactivity ">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application></Manifest>

Then start with the following code:

Intent Intent = new Intent ();                Intent.setaction ("Com.weiyuan.test");                StartActivity (intent); 

Use custom Permissions in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.