Android 如何从IntentService开始活动并通过课程<;T>;来自另一个班级?
我想通过IntentService启动一个活动,但是catch是activity名称,或者类名将作为参数传递给IntentService 下面是我的代码块Android 如何从IntentService开始活动并通过课程<;T>;来自另一个班级?,android,android-intent,android-service,android-intentservice,android-bundle,Android,Android Intent,Android Service,Android Intentservice,Android Bundle,我想通过IntentService启动一个活动,但是catch是activity名称,或者类名将作为参数传递给IntentService 下面是我的代码块 public class Runner { Context context; public void startActivity() { Intent intent = new Intent(context, ActivityLauncher.class); intent.pu
public class Runner {
Context context;
public void startActivity() {
Intent intent = new Intent(context, ActivityLauncher.class);
intent.putExtra("caller", "Runner");
//CameraActivity is my activity which i want to start
// I will be giving other activities also in other parts of my code
intent.putExtra("class",CameraActivity.class);
context.startService(intent);
}
}
现在ActivityLauncher服务的代码如下
public class ActivityLauncher extends IntentService {
public ActivityLauncher(String name) {
super("ActivityLauncher");
}
protected void onHandleIntent(Intent intent) {
try{
Bundle b = intent.getExtras();
Class<?> c = (Class<?>) b.get("class");
Intent mainActivityIntent = new Intent(getBaseContext(), c.getClass());
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String intentType = intent.getExtras().getString("caller");
if(intentType == null)
return;
if(intentType.equals("Runner"))
getApplication().startActivity(mainActivityIntent);
} catch (Exception localException) {
Log.d("TAG", localException.getMessage());
}
}
}
公共类ActivityLauncher扩展了IntentService{
公共活动启动器(字符串名称){
超级(“活动启动器”);
}
受保护的手部内容无效(意图){
试一试{
Bundle b=intent.getExtras();
c类=(b类)获取(“类”);
Intent mainActivityIntent=新意图(getBaseContext(),c.getClass());
mainActivityIntent.addFlags(Intent.FLAG\u ACTIVITY\u NEW\u TASK);
String intentType=intent.getExtras().getString(“调用方”);
if(intentType==null)
返回;
if(intentType.equals(“Runner”))
getApplication().startActivity(mainActivityIntent);
}catch(异常localException){
Log.d(“TAG”,localException.getMessage());
}
}
}
请告诉我如何改进我的代码。以及如何获得解决方案。您是否考虑过PackageManager.getLaunchIntentForPackage?没有。。。我是android开发的新手。你能提供一个如何使用它的链接吗?你需要目标组件的软件包:你能提供一个代码示例吗?我尝试过使用PackageManager。但活动仍然没有启动。我提供的活动包是“com.example.testProject.CameraActivity”,其中CameraActivity是我想要启动的活动