java.lang.RuntimeException on NotifyBuilder



我有这段代码,我想有两个不同的操作,我想显示一个打开MainActivity的经典通知和另一个执行LogoutTask的通知,但我有这个java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

我向您展示我的NotificationBuilder、AsyncTask和错误。

通知方法

private void sendNotification(final String message) {
    if (!message.contains("password")) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_kleim_letter)
                .setContentTitle(TITLE_NAME)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;
        notificationManager.notify(m, notificationBuilder.build());
    } else {
        DoLogoutTask doLogoutTask = new DoLogoutTask(getBaseContext());
        doLogoutTask.execute();
        Intent intent = new Intent(MyGcmListenerService.this, Login.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(MyGcmListenerService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MyGcmListenerService.this)
                .setSmallIcon(R.drawable.ic_stat_kleim_letter)
                .setContentTitle(TITLE_NAME)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
}

异步任务

public class DoLogoutTask extends AsyncTask<Void, Void, Boolean> {
SoapPrimitive resultString;
String response;
SharedPreferences mSharedPreferences;
SharedPreferences.Editor editor;
Context context;
ProgressDialog progressDialog;
public DoLogoutTask(Context context) {
    this.context = context;
    this.progressDialog = new ProgressDialog(context, R.style.progress_dialog_kleim);
}
@Override
protected void onPreExecute() {
    this.progressDialog.show();
    this.progressDialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
    this.progressDialog.setCancelable(false);
    this.progressDialog.getWindow().setGravity(Gravity.BOTTOM);
    this.progressDialog.setIndeterminate(true);
}
@Override
protected Boolean doInBackground(Void... params) {
    return doLogout();
}
@Override
protected void onPostExecute(Boolean result) {
    if (progressDialog != null && progressDialog.isShowing())
        progressDialog.dismiss();
}
public Boolean doLogout() {
    Boolean isLoggedOut = null;
    mSharedPreferences = context.getSharedPreferences(PreferencesUtility.MYPREFERENCES, Context.MODE_PRIVATE);
    String URL = URL;
    String METHOD = METHOD;
    String NAMESPACE = NAMESPACE;
    String SOAP_ACTION = ACTION;
    String idCliente = mSharedPreferences.getString(PreferencesUtility.IDCLIENTE, PreferencesUtility.VALUENOTFOUND);
    String regid = mSharedPreferences.getString(PreferencesUtility.REGID, PreferencesUtility.VALUENOTFOUND);
    JSONObject parent = new JSONObject();
    JSONObject logout = new JSONObject();
    JSONArray jsonArray = new JSONArray();
    try {
        logout.put("idCliente", idCliente);
        logout.put("regid", regid);
        parent.put("cliente", jsonArray);
        jsonArray.put(logout);
        String parentString = parent.toString();
        PropertyInfo paramPI = new PropertyInfo();
        paramPI.setName("infoCliente");
        paramPI.setValue(parentString);
        paramPI.setType(String.class);
        SoapObject request = new SoapObject(NAMESPACE, METHOD);
        request.addProperty(paramPI);
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapSerializationEnvelope.dotNet = true;
        soapSerializationEnvelope.setOutputSoapObject(request);
        HttpTransportSE transportSE = new HttpTransportSE(URL);
        transportSE.call(SOAP_ACTION, soapSerializationEnvelope);
        resultString = (SoapPrimitive) soapSerializationEnvelope.getResponse();
        response = resultString.toString();
        isLoggedOut = !response.contains("error");
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (HttpResponseException e) {
        e.printStackTrace();
    } catch (SoapFault soapFault) {
        soapFault.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return isLoggedOut;
    }
}

错误

12-10 11:09:53.486 19138-19390/it.pent.kleim E/AndroidRuntime:致命异常:异步任务#3工艺:it.penta.kleim,PID:19138java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序在android.os.Handler上。(Handler.java:200)在android.os.Handler上。(Handler.java:114)在android.app.Dialog.(Dialog.java:119)在android.app.AlertDialog.(AlertDialog.java:200)在android.app.AlertDialog.(AlertDialog.java:196)在android.app.ProgressDialog.(ProgressDialog.java:82)at it。penta.keim.16.retro_asynctasks.DoLogoutTask。(DoLogoutTask.java:44)at it.penta.keim.16.retro_gcm.MyGcmListenerService.sendNotification(MyGcmListenerServices.java:107)at it.penta.keim.16.retro_gcm.MyGcmListenerService.onMessageReceived(MyGcmListenerServices.java:75)网址:com.google.android.gms.gcm.GcmListenerService.zzq(未知来源)网址:com.google.android.gms.gcm.GcmListenerService.zzp(未知来源)网址:com.google.android.gms.gcm.GcmListenerService.zzo(未知来源)网址:com.google.android.gms.gcm.GcmListenerService.zza(未知来源)网址:com.google.android.gms.gcm.GcmListenerService$1.run(未知来源)位于java.util.concurrent.ThreadPoolExecutiator.runWorker(ThreadPoolExecutiator.java:1113)位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)在java.lang.Thread.run(Thread.java:818)

您的sendNotification方法是由执行onMessageReceived的线程池中的线程调用的,因此您不能从那里访问视图。为了接触视图,您应该从主线程获得一个Handler(您可以使用Looper.getMainLooper())。请检查http://developer.android.com/training/multiple-threads/communicate-ui.html

这里有一个快速示例:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
    @Override
    public void run() {
        //do stuff on the main thread
    }
});

您应该在run()方法

中移动与对话框相关的所有内容

相关内容

  • 没有找到相关文章

最新更新