使用类名启动 Activity Android
Posted
技术标签:
【中文标题】使用类名启动 Activity Android【英文标题】:Start Activity Android with class name 【发布时间】:2013-08-12 05:24:50 【问题描述】:我正在使用以下代码开始设置我想启动由android ins启动的设置活动
PackageList allowedAppsPackageName=CallHelper.Ds.getPackageList();
PackageManager manager = CallDetectService.packageManager;
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));
final int count = apps.size();
ResolveInfo info=new ResolveInfo();;
GridViewAppList.clear();
for (int i = 0; i < count; i++)
info= apps.get(i);
if(info.activityInfo.applicationInfo.packageName.contains("setting"))
break;
ApplicationInfo application = new ApplicationInfo();
application.title = info.loadLabel(manager);
application.setActivity(new ComponentName(
packageName,
info.activityInfo.name),
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
application.icon = info.activityInfo.loadIcon(manager);
application.packagename=packageName;
Log.i("PKG", application.packagename+" "+packageName+" "+info.activityInfo.name);
GridViewAppList.add(application);
【问题讨论】:
【参考方案1】:你可以用这个sn-p打开Settings
Activity:
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings"));
startActivity(intent);
如果你不知道你要打开什么Activity,你可以找到它的名字和包名,就像我提到的here。
【讨论】:
如果我知道类名,这是k,但如果我不知道怎么办?【参考方案2】:Settings
类中有许多操作常量,可用于创建一个意图,该意图将为所提供的子设置启动一个活动,或者根据操作简单地启动设置应用程序。
例如:
intent = new Intent(Settings.ACTION_SETTINGS); // ACTION_SETTINGS will start the Settings app
intent = new Intent(Settings.ACTION_WIFI_SETTINGS); // ACTION_WIFI_SETTINGS will show the WiFi settings
【讨论】:
以上是关于使用类名启动 Activity Android的主要内容,如果未能解决你的问题,请参考以下文章
Android-----Intent中通过startActivity(Intent intent )显式启动新的Activity
Android开发- Intent和Broadcast Receiver
bug_ _java.lang.RuntimeException: Unable to start activity ComponentInfo{包名/类名}