WI已在我的Android应用中实施推送通知。
我从我的服务器发送通知,当Androide设备收到通知时,我的sWakeLock对象上有一个空指针。
此外,我从未调用runIntentInService,因为我不知道怎么做!
这是调用堆栈:
10-26 13:31:55.410: E/AndroidRuntime(15351): FATAL EXCEPTION: IntentService[71715548489]
10-26 13:31:55.410: E/AndroidRuntime(15351): java.lang.NullPointerException
10-26 13:31:55.410: E/AndroidRuntime(15351): at com.example.android.stackwidget.GCMIntentService.onHandleIntent(GCMIntentService.java:42)
10-26 13:31:55.410: E/AndroidRuntime(15351): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-26 13:31:55.410: E/AndroidRuntime(15351): at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 13:31:55.410: E/AndroidRuntime(15351): at android.os.Looper.loop(Looper.java:137)
10-26 13:31:55.410: E/AndroidRuntime(15351): at android.os.HandlerThread.run(HandlerThread.java:60)
以下是我的GCMIntentService类的源代码:
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
public class GCMIntentService extends IntentService {
public GCMIntentService() {
super(Utils.GCMSenderId);
}
private static PowerManager.WakeLock sWakeLock;
private static final Object LOCK = GCMIntentService.class;
public String regIdtoSend = "";
@Override
protected void onHandleIntent(Intent intent) {
try {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
handleRegistration(intent);
} else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(intent);
}
}
finally {
synchronized(LOCK) {
sWakeLock.release(); //Null pointer here on sWakeLock
}
}
}
private void handleRegistration(Intent intent) {
String regId = intent.getStringExtra("registration_id");
Utils.registrationId = regId;
regIdtoSend = regId;
new LongOperation().execute("");
Log.e("", "registration id : "+regId);
}
private void handleMessage(Intent intent) {
Utils.notiMsg = intent.getStringExtra("message");
Context context = getApplicationContext();
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.ic_action_lab_white;
CharSequence tickerText = Utils.notiMsg;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "New notification";
CharSequence contentText = Utils.notiMsg;
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);
Utils.notificationReceived=true;
}
static void runIntentInService(Context context,Intent intent){
synchronized(LOCK) {
if (sWakeLock == null) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock");
}
}
sWakeLock.acquire();
intent.setClassName(context, GCMIntentService.class.getName());
context.startService(intent);
}
}