将对象从Service传递给MainActivity

时间:2015-05-28 07:30:16

标签: android

我从MainActivity开始我的TrackingService。我正在尝试从MainActivity启动AlertBuild.Dialog以检查GPS是否已关闭或打开。问题是我的LocationManger对象在TrackingService类中如何将此对象传递给MainActivity以启动AlterDialog.Builder?

MainActivity中的onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = new Intent(this, TrackingService.class);
    startService(i);

}

TrackingService类的一些代码:

       @Override
        public int onStartCommand(Intent intent, int flags, int startId) {       

            detectLocation();

            return START_STICKY;
        }

        private void detectLocation() {

            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            LocationListener ll = new myLocationListener(this);
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000, 0,
                    ll);
            enableGPS(lm);


        }

    private void enableGPS(LocationManager lm) {
        // Check wheather the GPS is enabled.
        if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, "GPS is Enabled in your devide",
                    Toast.LENGTH_SHORT).show();
        } else {
            showGPSDisabledAlertToUser();
        }
    }

    private void showGPSDisabledAlertToUser() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder
                .setMessage(
                        "GPS is disabled in your device. Would you like to enable it?")
                .setCancelable(false)
                .setPositiveButton("Goto Settings Page To Enable GPS",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent callGPSSettingIntent = new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(callGPSSettingIntent);
                            }
                        });
}

尝试从TrackingService类启动Dialog时出错:

05-28 09:47:50.223: E/AndroidRuntime(18946): FATAL EXCEPTION: main
05-28 09:47:50.223: E/AndroidRuntime(18946): Process: com.bustracker, PID: 18946
05-28 09:47:50.223: E/AndroidRuntime(18946): java.lang.RuntimeException: Unable to start service com.bustracker.TrackingService@361f3c16 with Intent { cmp=com.bustracker/.TrackingService }: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-28 09:47:50.223: E/AndroidRuntime(18946):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3326)
05-28 09:47:50.223: E/AndroidRuntime(18946):    at android.app.ActivityThread.access$2200(ActivityThread.java:177)
05-28 09:47:50.223: E/AndroidRuntime(18946):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1546)
05-28 09:47:50.223: E/AndroidRuntime(18946):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-28 09:47:50.223: E/AndroidRuntime(18946):    at android.os.Looper.loop(Looper.java:145)
05-28 09:47:50.223: E/AndroidRuntime(18946):    at android.app.ActivityThread.main(ActivityThread.java:5944)
05-28 09:47:50.223: E/AndroidRuntime(18946):    at java.lang.reflect.Method.invoke(Native Method)
05-28 09:47:50.223: E/AndroidRuntime(18946):    at java.lang.reflect.Method.invoke(Method.java:372)
05-28 09:47:50.223: E/AndroidRuntime(18946):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
05-28 09:47:50.223: E/AndroidRuntime(18946):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
05-28 09:47:50.223: E/AndroidRuntime(18946): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-28 09:47:50.223: E/AndroidRuntime(18946):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:691)