我想通过 IntentService 启动一个活动,但要注意的是活动名称或类名将作为参数传递给 IntentService。
以下是我的代码块...
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 Service的代码如下......
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());
}
}
}
请告诉我如何改进我的代码。我怎样才能得到解决方案。