简体   繁体   中英

How to start an activity in my broadcast receiver class?

In my broadcast receiver activity I need to run some code when an alert is fired which I can get working no problem, but I need to make sure my Map activity is on the screen when this code is run, so I am trying to start the Map activity with an intent but this crashes the application and I do not know why. Here is the broadcast receiver class:

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

In the accessMap method you can see where I am trying to start the map activity via an intent but the app crashes, here is what I copied from the 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

I spent a few hours experimenting and reading Android documentation and I am not sure if it is possible to start an activity via intent inside a broadcast receiver event, according to this page. When the user reaches a destination the user is shown some options on the map activity screen, but if the alert fires when they are on another screen these options will never show on the map screen, that is why I am trying to make sure the Map screen is started before the rest of the code in the accessMap method is executed. Can anyone suggest how I should proceed to fix this?

You should pass the variables though intent

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

You can create a Notification and show it. If the user clicks on the Notification then you can launch your Map screen.

Set the flag FLAG_ACTIVITY_NEW_TASK on the Intent .

But you really shouldn't be launching activity in this way. An activity seemingly popping up out of nowhere is a weird/undesirable user experience. The notification you are setting should be sufficient, then the user can decide whether he/she wants to launch your app. Just my 2¢ :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM