Android there is a way to start an application programatically without adding to manifest?

一笑奈何 提交于 2019-12-23 03:32:18

问题


I would like only to start some applications dynamically with the package name from the app i develop, i already tried this:

intent = new Intent(Intent.ACTION_VIEW);
component = new ComponentName(getApplicationContext(), myPrefs.getString("Combo1", null));
Log.i("LOG", myPrefs.getString("Combo1", null));
intent.setComponent(component);

and

intent = pm.getLaunchIntentForPackage(myPrefs.getString("Combo1", null));
startActivity(intent);

both works and several applications opens but for others i get this error.

11-29 21:42:08.723: E/AndroidRuntime(4719): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.lock/com.sec.android.app.phoneutil}; have you declared this activity in your AndroidManifest.xml?

I understand the error(that i need to add the app/activity in manifest), so i want to know if is a workaround to add dynamically activities to manifest, or there is a way to start dynamically an application without adding to the manifest that app/activity, or there is a permission that allow to open applications ?

From my research it seems impossible, but i want to know the opinion of an expert.

Thanx in advance.


回答1:


If you are talking about launching other apps from yours, this is the code you need:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageName,mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);

You can get the main activity like this:

Intent mIntent = ctx.getPackageManager().getLaunchIntentForPackage(packageName); 
if (mIntent != null) {
    if (mIntent.getComponent() != null) {
    mainActivity = mIntent.getComponent().getClassName();
    }
}



回答2:


You can not start an activity (screen) without adding it to the manifest! ever!

Always add it!




回答3:


Intent i = context.getPackageManager().getLaunchIntentForPackage("applications package name which you want to open"); context.startActivity(i);



来源:https://stackoverflow.com/questions/8317077/android-there-is-a-way-to-start-an-application-programatically-without-adding-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!