开发者

Android protected Activity as inner class

开发者 https://www.devze.com 2023-03-05 13:15 出处:网络
I want to have an activity that can only be launched from certain other activites in my app, and not from others

I want to have an activity that can only be launched from certain other activites in my app, and not from others

At the moment, this works:

public abstract class Launcher extends Activity {

protected static boolean flag = true;

protected abstract void Launch();

public static class myPrivateActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(flag)
            finish();
        setContentView(R.layout.main);


    }

}

}

public class SpecialActivity extends Launcher {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newlayout);
}   

@Override
protected void Launch {

    flag = false;

    Intent i = new Intent(Intent.ACTION_VIEW);
    String name = myPrivateActivity.class.getName();
    i.setClassName(this, name );
    startActivity(i);

    //finish();

}

}

and in android manifest

<activity android:name=".Launcher开发者_运维知识库$myPrivateActivity"
android:label="Private?">

However, I'd like to know if there is a better solution than just using a flag?


That's a creative approach. I don't think I've ever seen something like that before.

However, I don't think it accomplishes what you're trying to accomplish. If an approach like this were taken, it would probably make sense to "harden" it further by making the abstract base class and its inner static class package private, and any concrete public subclasses final. With these changes, then I don't think the flag would any longer provide any sort of protection.

However (again), do be aware that Android's permissions model provides for custom permissions, and may be used to achieve what I think you want to do. In fact, the document at http://developer.android.com/guide/topics/security/security.html describes how to "enforce your own permissions" to configure "an application that wants to control who can start one of its activities" in the "Declaring and Enforcing Permissions" section.

Does that get you closer to your goal?


I think you can use this:

android:permission

The name of a permission that clients must have to launch the activity or otherwise get it to respond to an intent. If a caller of startActivity() or startActivityForResult() has not been granted the specified permission, its intent will not be delivered to the activity. If this attribute is not set, the permission set by the element's permission attribute applies to the activity. If neither attribute is set, the activity is not protected by a permission.

http://developer.android.com/guide/topics/manifest/activity-element.html#prmsn

0

精彩评论

暂无评论...
验证码 换一张
取 消