如何在我的广播接收器课程中开始一项活动?

时间:2013-04-06 18:21:58

标签: android android-intent broadcastreceiver

在我的广播接收器活动中,我需要在发出警报时运行一些代码,我可以正常工作没有问题,但我需要确保在运行此代码时我的Map活动在屏幕上,所以我正在尝试用意图启动Map活动但是这会使应用程序崩溃,我不知道为什么。这是广播接收器类:

public class ProximityIntentReceiver extends BroadcastReceiver {
    private static final int NOTIFICATION_ID = 1000;
    Map mp = new Map();
    String con;
    int idpassed = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
    String key = LocationManager.KEY_PROXIMITY_ENTERING;
    Boolean entering = intent.getBooleanExtra(key, false);
    if (entering) {

        Log.d(getClass().getSimpleName(), "entering");
    } else {
        Log.d(getClass().getSimpleName(), "exiting");
    }

    con = intent.getStringExtra("mc");
    idpassed = intent.getIntExtra("id", 0);

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

    Intent notificationIntent = new Intent(context, NoteEdit.class);
    long ii = Long.valueOf(idpassed);
    notificationIntent.putExtra(MapDbAdapter.KEY_ROWID, ii);
    PendingIntent pendingIntent = PendingIntent.getActivity(context,
        intent.getIntExtra("id", 0), notificationIntent,
        PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = createNotification(context);

    String glat = String.valueOf(intent.getDoubleExtra("gl", 0.0));
    String g = String.valueOf(Map.goingToLat);

    // Depending on the type of marker fire a certain notification
    if ((intent.getStringExtra("mc").equals("contact"))
        && (!g.equals(glat))) {
        notification.setLatestEventInfo(context, "CONTACT",
            "You are approaching " + intent.getStringExtra("title"),
            pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);
    }

    else if ((intent.getStringExtra("mc").equals("contact"))
        && (g.equals(glat))) {
        notification.setLatestEventInfo(context, "DESTINATION",
            "You are approaching " + intent.getStringExtra("title"),
            pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);

        accessMap(context);
    }

    if ((intent.getStringExtra("mc").equals("park")) && (!g.equals(glat))) {
        notification.setLatestEventInfo(context, "PARK",
            "You are approaching " + intent.getStringExtra("title"),
            pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);
    }

    else if ((intent.getStringExtra("mc").equals("park"))
        && (g.equals(glat))) {
        notification.setLatestEventInfo(context, "DESTINATION",
            "You are approaching " + intent.getStringExtra("title"),
            pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);

        accessMap(context);
    }

    if ((intent.getStringExtra("mc").equals("food")) && (!g.equals(glat))) {
        notification.setLatestEventInfo(context, "FOOD",
            "You are approaching " + intent.getStringExtra("title"),
            pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);
    }

    else if ((intent.getStringExtra("mc").equals("food"))
        && (g.equals(glat))) {
        notification.setLatestEventInfo(context, "DESTINATION",
            "You are approaching " + intent.getStringExtra("title"),
            pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);

        accessMap(context);
    }

    if ((intent.getStringExtra("mc").equals("bar")) && (!g.equals(glat))) {
        notification.setLatestEventInfo(context, "BAR",
            "You are approaching " + intent.getStringExtra("title"),
            pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);
    }

    else if ((intent.getStringExtra("mc").equals("bar"))
        && (g.equals(glat))) {
        notification.setLatestEventInfo(context, "DESTINATION",
            "You are approaching " + intent.getStringExtra("title"),
            pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);

        accessMap(context);
    }

    if ((intent.getStringExtra("mc").equals("shopping"))
        && (!g.equals(glat))) {
        notification.setLatestEventInfo(context, "SHOPPING",
            "You are approaching " + intent.getStringExtra("title"),
            pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);
    }

    else if ((intent.getStringExtra("mc").equals("shopping"))
        && (g.equals(glat))) {
        notification.setLatestEventInfo(context, "DESTINATION",
            "You are approaching " + intent.getStringExtra("title"),
            pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);

        accessMap(context);
    }

    if ((intent.getStringExtra("mc").equals("caution"))
        && (!g.equals(glat))) {
        notification.setLatestEventInfo(
            context,
            "CAUTION",
            "Beware, you are approaching "
                + intent.getStringExtra("title"), pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);
    }

    else if ((intent.getStringExtra("mc").equals("caution"))
        && (g.equals(glat))) {
        notification.setLatestEventInfo(
            context,
            "DESTINATION",
            "Beware, you are approaching "
                + intent.getStringExtra("title"), pendingIntent);
        notificationManager.notify(intent.getIntExtra("id", -1),
            notification);

        accessMap(context);
    }

    }

    public void accessMap(Context context) {

    Intent openNext = new Intent("com.timer.MAP");
    context.startActivity(openNext);

    Map.destination = null;
    Map.goingToLat = 0;
    Map.mtv.setText("Select a new Destination!");
    Map.scanOptions();
    Map.num = 0;
    Map.playr.stop();
    Map.playr.reset();
    Map.playr.release();
    Map.handler.removeCallbacks(Map.getRunnable());
    Map.passing = true;
    }

    private Notification createNotification(Context context) {
    Notification notification = new Notification();

    if (con.equals("contact")) {

        notification.icon = R.drawable.contact;
        notification.sound = Uri
            .parse("android.resource://com.example.newmaps/"
                + R.raw.contact);

    }

    else if (con.equals("caution")) {

        notification.icon = R.drawable.caution;
        notification.sound = Uri
            .parse("android.resource://com.example.newmaps/"
                + R.raw.caution);
    }

    else if (con.equals("shopping")) {

        notification.icon = R.drawable.shopping;
        notification.sound = Uri
            .parse("android.resource://com.example.newmaps/"
                + R.raw.shopping);
    }

    else if (con.equals("bar")) {

        notification.icon = R.drawable.bar;
        notification.sound = Uri
            .parse("android.resource://com.example.newmaps/"
                + R.raw.bar);
    }

    else if (con.equals("park")) {

        notification.icon = R.drawable.park;
        notification.sound = Uri
            .parse("android.resource://com.example.newmaps/"
                + R.raw.park);
    }

    else if (con.equals("food")) {

        notification.icon = R.drawable.food;
        notification.sound = Uri
            .parse("android.resource://com.example.newmaps/"
                + R.raw.food);
    }

    notification.when = System.currentTimeMillis();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.ledARGB = Color.WHITE;
    notification.ledOnMS = 1500;
    notification.ledOffMS = 1500;
    return notification;
    }
}

在accessMap方法中,您可以看到我尝试通过意图启动地图活动但应用程序崩溃的位置,这是我从logcat复制的内容:

04-06 18:04:04.363: D/ProximityIntentReceiver(5836): entering
04-06 18:04:04.433: D/ProximityIntentReceiver(5836): entering
04-06 18:04:04.463: D/ProximityIntentReceiver(5836): entering
04-06 18:04:04.473: D/ProximityIntentReceiver(5836): entering
04-06 18:04:04.513: D/ProximityIntentReceiver(5836): entering
04-06 18:04:04.543: D/MediaPlayer(5836): stop() mUri is android.resource://com.example.newmaps/2131034121
04-06 18:04:04.853: D/ProximityIntentReceiver(5836): entering
04-06 18:04:04.993: I/System.out(5836): 1Bert
04-06 18:04:04.993: W/dalvikvm(5836): threadid=1: thread exiting with uncaught exception (group=0x4205c450)
04-06 18:04:05.003: E/AndroidRuntime(5836): FATAL EXCEPTION: main
04-06 18:04:05.003: E/AndroidRuntime(5836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newmaps/com.example.memoryGuide.Map}: java.lang.NullPointerException
04-06 18:04:05.003: E/AndroidRuntime(5836):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2090)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at android.app.ActivityThread.access$600(ActivityThread.java:136)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at android.os.Looper.loop(Looper.java:137)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at android.app.ActivityThread.main(ActivityThread.java:4802)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at java.lang.reflect.Method.invoke(Method.java:511)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at dalvik.system.NativeStart.main(Native Method)
04-06 18:04:05.003: E/AndroidRuntime(5836): Caused by: java.lang.NullPointerException
04-06 18:04:05.003: E/AndroidRuntime(5836):     at com.example.memoryGuide.Map.onCreate(Map.java:161)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at android.app.Activity.performCreate(Activity.java:5013)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-06 18:04:05.003: E/AndroidRuntime(5836):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
04-06 18:04:05.003: E/AndroidRuntime(5836):     ... 11 more
04-06 18:04:10.403: E/Trace(6312): error opening trace file: No such file or directory (2)
04-06 18:04:10.643: D/-heap(6312): GC_FOR_ALLOC freed 81K, 5% free 7809K/8195K, paused 26ms, total 31ms
04-06 18:04:10.703: D/-heap(6312): GC_CONCURRENT freed 1K, 4% free 11904K/12359K, paused 16ms+8ms, total 46ms
04-06 18:04:11.083: D/-heap(6312): GC_CONCURRENT freed 671K, 7% free 12580K/13511K, paused 14ms+18ms, total 52ms
04-06 18:04:11.363: I/System.out(6312): 1Bert
04-06 18:04:11.643: D/-heap(6312): GC_CONCURRENT freed 561K, 6% free 13606K/14407K, paused 24ms+5ms, total 86ms
04-06 18:04:11.803: I/Adreno200-EGL(6312): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_RB1.04.01.01.06.043_msm7627a_JB_REL_RB1.2_Merge_release_AU (Merge)
04-06 18:04:11.803: I/Adreno200-EGL(6312): Build Date: 12/10/12 Mon
04-06 18:04:11.803: I/Adreno200-EGL(6312): Local Branch: 
04-06 18:04:11.803: I/Adreno200-EGL(6312): Remote Branch: m/jb_rel_rb1.2
04-06 18:04:11.803: I/Adreno200-EGL(6312): Local Patches: NONE
04-06 18:04:11.803: I/Adreno200-EGL(6312): Reconstruct Branch: NOTHING
04-06 18:04:12.103: D/-heap(6312): GC_FOR_ALLOC freed 1599K, 14% free 13626K/15815K, paused 27ms, total 37ms
04-06 18:04:12.793: D/-heap(6312): GC_FOR_ALLOC freed 1067K, 11% free 14190K/15815K, paused 42ms, total 47ms
04-06 18:04:13.553: D/-heap(6312): GC_CONCURRENT freed 1799K, 16% free 14320K/16903K, paused 25ms+28ms, total 90ms
04-06 18:04:14.783: D/-heap(6312): GC_CONCURRENT freed 1546K, 14% free 14702K/16903K, paused 13ms+17ms, total 67ms
04-06 18:04:15.943: D/-heap(6312): GC_CONCURRENT freed 1555K, 11% free 15075K/16903K, paused 12ms+17ms, total 72ms
04-06 18:04:16.733: D/-heap(6312): GC_CONCURRENT freed 1666K, 11% free 15474K/17351K, paused 13ms+26ms, total 106ms

根据this页面,我花了几个小时试验和阅读Android文档,我不确定是否可以在广播接收器事件中通过意图启动活动。当用户到达目的地时,用户会在地图活动屏幕上显示一些选项,但如果警报在另一个屏幕上显示,则这些选项将永远不会显示在地图屏幕上,这就是为什么我要确保地图在执行accessMap方法中的其余代码之前启动屏幕。任何人都可以建议我应该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

你应该通过意图传递变量

public void accessMap(Context context) {
Intent openNext = new Intent("com.timer.MAP");
openNext.putExtra("destination", null);
openNext.putExtra("goingToLat", 0);
openNext.putExtra("text for mtv", "Select a new Destination!");
.........
openNext.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openNext);
}

答案 1 :(得分:1)

您可以创建通知并显示它。如果用户点击通知,则可以启动地图屏幕。

答案 2 :(得分:1)

FLAG_ACTIVITY_NEW_TASK上设置标记Intent

但你真的不应该以这种方式发起活动。似乎突然出现的活动是一种奇怪/不受欢迎的用户体验。您设置的通知应该足够,然后用户可以决定是否要启动您的应用。只是我2¢:D