[英]How to launch Activity using intent which passed from Service
I have a Service
which obtains a class name in a string variable and passes to sharedpreferences using this code: 我有一个
Service
,该Service
在字符串变量中获取类名称,并使用以下代码传递给sharedpreferences:
settings.edit().putString("class", activityClass).commit();
` `
the service then launches my passprompt activity, which needs to read the String and launch the activity stored inside. 然后该服务启动我的密码提示活动,该活动需要读取String并启动存储在其中的活动。
try {
SharedPreferences settings = getApplicationContext().getSharedPreferences("data", Context.MODE_PRIVATE);
String c = settings.getString("class", "null");
Intent newIntent= new Intent(passprompt.this, Class.forName(c));
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
}
catch (ClassNotFoundException e) {
}
however the intended activity is not launching when expected 但是预期的活动没有在预期的时候启动
You may try this , 你可以试试看
If this activity with in your app Get the fully qualified Activity class name and put it into intent extra as follows, 如果此活动包含在您的应用中,请获取完全限定的Activity类名称,并按如下所示将其放入意向中,
Intent newIntent= new Intent(Passprompt.this, Class.forName("your activity class name"))); newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(newIntent);
If this is activity within another applicatino 如果这是另一个应用程序中的活动
Intent intent = new Intent(Intent.ACTION_MAIN); intent.setComponent(new ComponentName("packagename", "packagename.launh_activity_name")); startActivity(intent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.