私はこのコードを持っています.2つの異なるアクションが必要です.MainActivityを開くための1つの古典的な通知とLogoutTaskを行うための別の通知を表示したいのですが,私はこのjava.lang.RuntimeException: Can't create handler inside threadを持っています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.penta.kleim E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3 プロセス: it.penta.kleim、PID: 19138 java.lang.RuntimeException: Can't create handler inside 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) で it.penta.kleim.sixteen.retro_asynctasks.DoLogoutTask.(DoLogoutTask.java:44) it.penta.kleim.sixteen.retro_gcm.MyGcmListenerService.sendNotification(MyGcmListenerService.java:107) で it.penta.kleim.sixteen.retro_gcm.MyGcmListenerService.onMessageReceived(MyGcmListenerService.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. java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) で com.google.android.gms.gcm.GcmListenerService$1.run(未知のソース) でutil.concurrent.java.lang.Thread.run(Thread.java:818) で ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)