2

I want to start an activity through IntentService, but the catch is the activty name or the class name will be passed as parameter to the IntentService.

Following are my code blocks...

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);
    }

}

Now the code for ActivityLauncher Service is as follows.....

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());
    }
  }

 }

Please tell me how can i improve my code. and how can i get the solution.

  • Have you considered `PackageManager.getLaunchIntentForPackage`? – Karakuri Jun 13 '14 at 14:47
  • No... I m new to android development. Can you please provide a link of how to use it?? –  Jun 13 '14 at 14:49
  • You need the package of the target component: https://developer.android.com/reference/android/content/pm/PackageManager.html#getLaunchIntentForPackage(java.lang.String) – Karakuri Jun 13 '14 at 14:51
  • can you please give a code sample?? –  Jun 13 '14 at 14:52
  • i tried using PackageManager.But still the activity is not lauching. I m providing package of the activity as - "com.example.testProject.CameraActivity" where CameraActivity is my Activity i want to launch –  Jun 13 '14 at 15:08
  • You have to provide the package name that you used setting up project e.g. "com.example.testproject" and your CameraActivity must have CATEGORY_INFO or CATEGORY_LAUNCH specified in the AndroidManifext.xml. – Damian Petla Jun 13 '14 at 15:32
  • Anyway, what is the point to start activities through IntentService? Are you planning to start different activities at the same time and you want to be queued? – Damian Petla Jun 13 '14 at 15:39
  • i provided action info as - in manifest file. And calling activity as- Intent intent2 = packageManager.getLaunchIntentForPackage(packageName); intent2.setAction("com.example.testProject.LAUNCH_CAMERA"); startActivity(intent2); –  Jun 13 '14 at 15:39
  • actually i m starting activities from a non ui thread. if there is any other better way please tell me. it will be a great help.. n yes i will be starting different activites –  Jun 13 '14 at 15:42
  • Using Context from the background is not a good idea, unless it's application context. Assuming you have your Context working why don't you simply call startActivity() instead startService()? – Damian Petla Jun 16 '14 at 08:11

0 Answers0