Fix null pointer exception if launch intent may be null

This commit is contained in:
Sebastián Katzer 2014-11-04 16:51:32 +00:00
parent d0d8b9e981
commit 8b7ef48cff

View File

@ -21,6 +21,12 @@
package de.appplant.cordova.plugin.background; package de.appplant.cordova.plugin.background;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Notification; import android.app.Notification;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
@ -30,14 +36,6 @@ import android.content.res.Resources;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/** /**
* Puts the service in a foreground state, where the system considers it to be * Puts the service in a foreground state, where the system considers it to be
@ -92,6 +90,7 @@ public class ForegroundService extends Service {
@Override @Override
public void run() { public void run() {
handler.post(new Runnable() { handler.post(new Runnable() {
@Override
public void run() { public void run() {
// Nothing to do here // Nothing to do here
// Log.d("BackgroundMode", "" + new Date().getTime()); // Log.d("BackgroundMode", "" + new Date().getTime());
@ -119,26 +118,27 @@ public class ForegroundService extends Service {
* A local ongoing notification which pending intent is bound to the * A local ongoing notification which pending intent is bound to the
* main activity. * main activity.
*/ */
@SuppressLint("NewApi")
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private Notification makeNotification() { private Notification makeNotification() {
JSONObject settings = BackgroundMode.settings;
Context context = getApplicationContext(); Context context = getApplicationContext();
String pkgName = context.getPackageName(); String pkgName = context.getPackageName();
Intent intent = context.getPackageManager() Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(pkgName); .getLaunchIntentForPackage(pkgName);
PendingIntent contentIntent = PendingIntent.getActivity(
context, NOTIFICATION_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
JSONObject settings = BackgroundMode.settings;
Notification.Builder notification = new Notification.Builder(context) Notification.Builder notification = new Notification.Builder(context)
.setContentTitle(settings.optString("title")) .setContentTitle(settings.optString("title", ""))
.setContentText(settings.optString("text")) .setContentText(settings.optString("text", ""))
.setTicker(settings.optString("ticker")) .setTicker(settings.optString("ticker", ""))
.setOngoing(true) .setOngoing(true)
.setSmallIcon(getIconResId()); .setSmallIcon(getIconResId());
if (settings.optBoolean("resume")) { if (intent != null && settings.optBoolean("resume")) {
PendingIntent contentIntent = PendingIntent.getActivity(
context, NOTIFICATION_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(contentIntent); notification.setContentIntent(contentIntent);
} }