我将此代码用于GCM,但是当我得到推送通知时,我得到了这样的错误:
> 08-24 09:28:31.670 25605-5683/icon.apkt E/AndroidRuntime﹕ FATAL
> EXCEPTION: IntentService[GCMIntentService-43597399078-11]
> android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
> at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5503)
> at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1099)
> at android.view.ViewGroup.invalidateChildFast(ViewGroup.java:4417)
> at android.view.View.invalidateViewProperty(View.java:11201)
> at android.view.ViewPropertyAnimator$AnimatorEventListener.onAnimationUpdate(ViewPropertyAnimator.java:1047)
> at android.animation.ValueAnimator.animateValue(ValueAnimator.java:1166)
> at android.animation.ValueAnimator.animationFrame(ValueAnimator.java:1102)
> at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1131)
> at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:616)
> at android.animation.ValueAnimator$AnimationHandler.run(ValueAnimator.java:639)
> at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
> at android.view.Choreographer.doCallbacks(Choreographer.java:591)
> at android.view.Choreographer.doFrame(Choreographer.java:560)
> at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
> at android.os.Handler.handleCallback(Handler.java:725)
> at android.os.Handler.dispatchMessage(Handler.java:92)
> at android.os.Looper.loop(Looper.java:137)
> at android.os.HandlerThread.run(HandlerThread.java:60)
这是我的代码:
/**
* Method called on device un registred
*/
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "GCM Device unregistered");
generateNotification(context, "Device Unregistered", new Intent());
ServerUtilities.unregister(context, registrationId);
}
/**
* Method called on Receiving a new message
*/
@Override
protected void onMessage(final Context context, Intent intent) {
Log.i(TAG, "GCM Received message");
if (intent == null) {
} else {
String message = intent.getExtras().getString("message");
String namaPelanggan = "", alamat = "", telepon = "", noGangguan = "";
// {
// "reportNumber" : "G5313111100001"
// "status" : "Dalam Perjalanan",
// "poskoId" : "538711",
// "poskoName" : "POSKO DEPOK KOTA",
// "reguId" : "6683",
// "reguName" : "rdpk55",
// "idPel" : "525060659230",
// "namaPelanggan" : "SUPRIYADI" ,
// "createBy" : "11387",
// "createName" : "POSKODEPOK",
// "namaPelapor" : ""SAMINAH,
// "alamat" : "KP PANCORAN MAS",
// "telepon" : "021234234",
// "hp" : "081234234234",
// "longitude" : "0",
// "latitude" : "0"
// }
try {
JSONObject json = new JSONObject(message);
namaPelanggan = json.getString("namaPelanggan");
alamat = json.getString("alamat");
telepon = json.getString("telepon");
noGangguan = json.getString("reportNumber");
String total = "Gangguan baru diterima, NoGangguan = " + noGangguan
+ " Pelapor = " + namaPelanggan + ", Lokasi = " + alamat
+ ", Telepon = " + telepon;
// final String nogang = noGangguan;
try {
// GenericMethods.ShowToast(context, total);
User user = new User();
if (user.fromLocal(context)) {
GenericMethods.getGangguanOnline(context, user,
new onFinish() {
@Override
public void complete() {
// DO NOTHING
// Intent intent = null;
//
// Gangguan tugas = GenericMethods
// .getGangguan(nogang);
//
// if (tugas == null) {
// Log.i("RETURN", "NULL");
// return;
// }
//
// Log.i("SELECT", tugas.LASTSTATUS);
// if (tugas.LASTSTATUS
// .equalsIgnoreCase(WSDLConfig.status.status0))
// {
// intent = new Intent(context,
// BerangkatActivity.class);
// Log.d("GCM Open", tugas.LASTSTATUS);
// } else if (tugas.LASTSTATUS
// .equalsIgnoreCase(WSDLConfig.status.status1))
// {
// intent = new Intent(context,
// SebelumActivity.class);
// Log.d("GCM Open", tugas.LASTSTATUS);
// } else if (tugas.LASTSTATUS
// .equalsIgnoreCase(WSDLConfig.status.status2))
// {
// intent = new Intent(context,
// SesudahActivity.class);
// Log.d("GCM Open", tugas.LASTSTATUS);
// } else if (tugas.LASTSTATUS
// .equalsIgnoreCase(WSDLConfig.status.status3))
// {
// intent = new Intent(context,
// SesudahActivity.class);
// Log.d("GCM Open", tugas.LASTSTATUS);
// } else if (tugas.LASTSTATUS
// .equalsIgnoreCase(WSDLConfig.status.status4))
// {
// intent = new Intent(context,
// SelesaiActivity.class);
// Log.d("GCM Open", tugas.LASTSTATUS);
// }
//
// if (intent != null) {
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.putExtra(
// Configuration.put_runworkflowID,
// tugas.REPORTNUMBER);
// startActivity(intent);
// }
// if (GenericMethods.activeActivity != null)
// ((MainActivity) GenericMethods.activeActivity)
// .getDaftarGangguanOffline();
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
DBHelper db = new DBHelper(context);
db.insertGCMList(noGangguan, total);
db.close();
Log.i("GCM","GCM Notif");
generateNotification(getApplicationContext(), total, new Intent(
getApplicationContext(), MainActivity.class));
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
该异常不是关于GCM,而是因为您尝试在意图服务中更新UI,意图服务实际上在单独的线程中运行。 Android不允许在非主线程中更新UI。
尝试将更新UI代码移动到主线程中,这里只是一个示例:
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
// put your update UI code here.
}
});