Kevin
Kevin

Reputation: 1950

Notification runs then immediately crashes

I have proximity alerts set up. (ADDED CRASH LOG)

When the proximity alert fires, I want a notification to load. Then when the user clicks the notification, a new activity/form should load.

Right now, the notification runs then the app crashes. The notification text is received and displayed successfully though.

Does anyone see something wrong with my code that would cause it to crash when firing the notification?

This code sets up the proximity alert/notification

    public void SetupProximityAlerts(LocationManager lm, String name, String info, double latitude, double longitude, int range)
{
    Intent intent = new Intent(PROX_ALERT_INTENT);
    intent.putExtra("Name", name);
    intent.putExtra("Info", info);

    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    lm.addProximityAlert(latitude, longitude, range, -1, pi);
    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);

    registerReceiver(new MyBroadcastReceiver(), filter);
}

And here is my BroadcastReceiver which sets the data to display in the notification. The app crashes when running: nm.notify(0, n);

Everything up until nm.notify(0, n); runs successfully. I've verified this with logging.

public class MyBroadcastReceiver extends BroadcastReceiver
 {

@Override
public void onReceive(Context context, Intent intent)
{
    Bundle extras = intent.getExtras();

    String deal = (String) extras.get("Info");

    Intent notificationIntent = new Intent(context, ViewTarget.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Resources res = context.getResources();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentIntent(contentIntent).setSmallIcon(R.drawable.icon).setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.icon)).setTicker(deal).setWhen(System.currentTimeMillis()).setAutoCancel(true).setContentTitle("Message")
            .setContentText(deal);
    Notification n = builder.getNotification();

    n.defaults |= Notification.DEFAULT_ALL;

    nm.notify(0, n);
}
}

And here is the activity I want to load when the user clicks the notification

public class ViewTarget extends ListActivity
{

@Override
public ListAdapter getListAdapter()
{
    // TODO Auto-generated method stub
    return super.getListAdapter();
}

@Override
public ListView getListView()
{
    // TODO Auto-generated method stub
    return super.getListView();
}

@Override
public void setListAdapter(ListAdapter adapter)
{
    // TODO Auto-generated method stub
    super.setListAdapter(adapter);

}

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locations);
    Log.v("db", "Inside ViewTarget");
}

}

Here is the crash log

04-17 21:53:52.407: E/AndroidRuntime(4717): FATAL EXCEPTION: main
04-17 21:53:52.407: E/AndroidRuntime(4717): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.kjdv.gpsVegas.MyBroadcastReceiver (has extras) } in com.kjdv.gpsVegas.MyBroadcastReceiver@44a4bc30
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:905)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.os.Handler.handleCallback(Handler.java:587)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.os.Looper.loop(Looper.java:123)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at java.lang.reflect.Method.invokeNative(Native Method)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at java.lang.reflect.Method.invoke(Method.java:521)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at dalvik.system.NativeStart.main(Native Method)
04-17 21:53:52.407: E/AndroidRuntime(4717): Caused by: java.lang.SecurityException: Requires VIBRATE permission
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.os.Parcel.readException(Parcel.java:1247)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.os.Parcel.readException(Parcel.java:1235)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.app.NotificationManager.notify(NotificationManager.java:110)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.app.NotificationManager.notify(NotificationManager.java:90)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at com.kjdv.gpsVegas.MyBroadcastReceiver.onReceive(MyBroadcastReceiver.java:51)
04-17 21:53:52.407: E/AndroidRuntime(4717):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:892)
04-17 21:53:52.407: E/AndroidRuntime(4717):     ... 9 more

Thanks! Kevin

Upvotes: 2

Views: 1023

Answers (2)

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

Caused by: java.lang.SecurityException: Requires VIBRATE permission

the above line in your crash indicate that you are using vibrate functionality then youmust add permission in manifeast file.The below is the permission needed to add

<uses-permission android:name="android.permission.VIBRATE" />

Upvotes: 1

Sam
Sam

Reputation: 86948

Caused by: java.lang.SecurityException: Requires VIBRATE permission

Did you forget a permission?

Upvotes: 3

Related Questions