android start activity from service

后端 未结 7 1219
傲寒
傲寒 2020-11-22 03:04

Android:

public class LocationService extends Service {

@Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startI         


        
7条回答
  •  Happy的楠姐
    2020-11-22 03:46

    I had the same problem, and want to let you know that none of the above worked for me. What worked for me was:

     Intent dialogIntent = new Intent(this, myActivity.class);
     dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     this.startActivity(dialogIntent);
    

    and in one my subclasses, stored in a separate file I had to:

    public static Service myService;
    
    myService = this;
    
    new SubService(myService);
    
    Intent dialogIntent = new Intent(myService, myActivity.class);
    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    myService.startActivity(dialogIntent);
    

    All the other answers gave me a nullpointerexception.

提交回复
热议问题