android中服务类的进程对话框

时间:2014-04-16 11:24:01

标签: android service handler

我正在尝试运行服务运行时运行进程对话框。我正在使用hanler在我的服务类中运行进程对话框。那个时候我有以下的问题。请任何人帮助我.....

logcat的

04-16 16:43:57.691: W/dalvikvm(395): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-16 16:43:57.813: E/AndroidRuntime(395): FATAL EXCEPTION: main
04-16 16:43:57.813: E/AndroidRuntime(395): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.ViewRoot.setView(ViewRoot.java:531)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.Dialog.show(Dialog.java:241)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ProgressDialog.show(ProgressDialog.java:95)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.appname.notification.StartNotification$1.handleMessage(StartNotification.java:235)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.os.Looper.loop(Looper.java:123)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-16 16:43:57.813: E/AndroidRuntime(395):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 16:43:57.813: E/AndroidRuntime(395):  at java.lang.reflect.Method.invoke(Method.java:507)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-16 16:43:57.813: E/AndroidRuntime(395):  at dalvik.system.NativeStart.main(Native Method)
04-16 16:43:57.813: E/AndroidRuntime(395): FATAL EXCEPTION: main
04-16 16:43:57.813: E/AndroidRuntime(395): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.ViewRoot.setView(ViewRoot.java:531)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.Dialog.show(Dialog.java:241)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ProgressDialog.show(ProgressDialog.java:95)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.appname.notification.StartNotification$1.handleMessage(StartNotification.java:235)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.os.Looper.loop(Looper.java:123)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-16 16:43:57.813: E/AndroidRuntime(395):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 16:43:57.813: E/AndroidRuntime(395):  at java.lang.reflect.Method.invoke(Method.java:507)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-16 16:43:57.813: E/AndroidRuntime(395):  at dalvik.system.NativeStart.main(Native Method)

服务类。

public class StartNotification extends Service {

    SharedPreferences sharedPreferences;
    String notification_Url = KP.KPURL.NOTIFICATION;
    public ProgressDialog pDialog;
    public Message m = new Message();
    private static final int PROCESS_START = 0;
    private static final int PROCESS_STOP = 1;

    // "2014-02-04 00:00:00"
    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        processLoading();
        return super.onStartCommand(intent, flags, startId);
    }
public void storelocalDB() {
// my logic
}

    private void processLoading() {
        m.what = PROCESS_START;
        handler.sendMessage(m);
        storelocalDB();
        m = new Message();
        m.what = PROCESS_STOP;
        handler.sendMessage(m);
    }
private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case PROCESS_START:
                pDialog = ProgressDialog.show(getApplicationContext(),
                        " Please wait...!", "Loading...", true, false);
                break;
            case PROCESS_STOP:
                if (pDialog.isShowing()) {
                    pDialog.dismiss();
                }
                break;
            }
        }
    };
}

2 个答案:

答案 0 :(得分:0)

首先尝试创建一个新对象:

ProgressDialog pdialog = new ProgressDialog(getActivity());

我没有看到你创建了一个对象。

答案 1 :(得分:0)

我认为你不能从服务中显示ProgressDialog。更好的选择是你必须在活动中显示对话框,从那里开始服务并从那里注册接收器。当服务停止向活动发送广播并完成正在运行的进度对话框