简体   繁体   中英

Android starting an activity from background service

public void onCreate()
{ 
    super.onCreate();
    Thread tt = new Thread(){
        public void run()
        {
           Intent dialogIntent = new Intent(getBaseContext(), ActivityClass.class);
           dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           getApplication().startActivity(dialogIntent);
        }
    };
    tt.start();
}

by using this way i am try to call my activity from service but my application crash please help me out

Ref Below link:

http://developer.android.com/guide/components/services.html

startService(i1); //Start Service.

Use startActivityForResult() instead of startActivity() because in service we use startActivityResult() method because broadcast receiver will give the response of onresultActivity.. so you could use this line

Use startActivityForResult(intent,requestcode ) instead of startActivity() .

Thank you..

Just do,

   public void onCreate() {
    super.onStart(intent, startId);
    Intent in=new Intent().setClass(yourserviceclass.this,ActivityClass.class);
     in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(in);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM