android start activity from service

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

Android:

public class LocationService extends Service {

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


        
7条回答
  •  我寻月下人不归
    2020-11-22 03:27

    Alternately,

    You can use your own Application class and call from wherever you needs (especially non-activities).

    public class App extends Application {
    
        protected static Context context = null;
    
        @Override
        public void onCreate() {
            super.onCreate();
            context = getApplicationContext();
        }
    
        public static Context getContext() {
            return context;
        }
    
    }
    

    And register your Application class :

    Then call :

    App.getContext();
    

提交回复
热议问题