2

我设置了接近警报。(添加崩溃日志)

当接近警报触发时,我想要加载通知。然后当用户点击通知时,应该加载一个新的活动/表单。

现在,通知运行,然后应用程序崩溃。通知文本已成功接收并显示。

有没有人看到我的代码有什么问题会导致它在触发通知时崩溃?

此代码设置接近警报/通知

    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);
}

这是我的 BroadcastReceiver,它设置要在通知中显示的数据。运行时应用崩溃:nm.notify(0, n);

直到 nm.notify(0, n); 成功运行。我已经通过日志验证了这一点。

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);
}
}

这是用户单击通知时我要加载的活动

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");
}

}

这是崩溃日志

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

谢谢!凯文

4

2 回答 2

3

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

你忘记许可了吗?

于 2012-04-18T02:22:09.730 回答
1
Caused by: java.lang.SecurityException: Requires VIBRATE permission

崩溃中的上述行表明您正在使用振动功能,那么您必须在清单文件中添加权限。以下是需要添加的权限

<uses-permission android:name="android.permission.VIBRATE" />
于 2012-04-18T02:26:03.640 回答