Как запустить Activity из IntentService и передать класс<T> из другого класса?

Я хочу начать действие через 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());
    }
  }

 }

Пожалуйста, скажите мне, как я могу улучшить свой код. и как я могу получить решение.

0 ответов

Другие вопросы по тегам