Android - 从状态栏切换到活动

时间:2011-11-01 00:24:34

标签: android service android-activity notifications alarmmanager

首先,我知道这个问题已被多次询问和回答。如果这些解决方案对我有用,我不会重新发布。

在我开始活动之前,我创建并切换了10个活动 开始闹钟。

然后我使用以下代码从最后一个活动(用户与之交互)中创建alarmManager:

        nextButton.setOnClickListener(new Button.OnClickListener(){ 
            @Override
            //Code adapted from example at android-er.blogspot.com
            public void onClick(View arg0) {
                //Declare the intent to start a new service
                Intent myIntent = new Intent(Page10.this, AlarmService.class);
                pendingIntent = PendingIntent.getService(Page10.this, 0, myIntent, 0);

                //Create the alarm manager and connect it to ALARM_SERVICE
                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                //Use the calendar to time the action of the alarm
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.SECOND, 10);

                //Set the alarm to buzz after the time defined in calendar
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
                Toast.makeText(Page10.this, "Start Alarm", Toast.LENGTH_LONG).show();

                //Start the main activity, the alarm service is now running in the background
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

创建闹钟后,我切换到主要活动,因为我希望闹钟在后台运行。传递给alarmManager的pendingIntent是以下服务:

public class AlarmService extends Service {
    @Override
    public void onCreate() {
        //Get a notification manager
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager SlugNotificationManager = (NotificationManager) getSystemService(ns);

        //Instantiate the notification
        int icon = R.drawable.slugmoodtextless;
        CharSequence tickerText = "Survey Ready";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);

        //Set notification messages and PendingIntent
        Context context = getApplicationContext();
        CharSequence contentTitle = "Slugmood";
        CharSequence contentText = "Survey Ready!";
        Intent notificationIntent = new Intent(this, Page1.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        //Pass notification to notification manager
        final int HELLO_ID = 1;
        SlugNotificationManager.notify(HELLO_ID, notification);

        Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
    }

为了简洁起见,我没有包含其他重写方法,因为我的服务不使用它们。所以我希望我的服务做的是在闹钟响起时向状态栏发送通知,然后用户可以点击通知返回到page1(创建的第二个活动)。一切正常,直到用户点击通知。然后程序崩溃了一个非描述性的“程序意外崩溃”并返回到page10之前的页面(不是page1,我设置pendingIntent)。

现在,我最初只是想让服务立即启动page1活动。当我从服务中启动page1活动时,使用通知系统解决了完全相同的崩溃问题。

我尝试过的解决方案:    - 设置意图上的各种标志,包括CLEAR_TASK,CLEAR_TOP,REORDER_ACTIVITY等    - 在尝试从活动中重新启动之前,确保完成所有活动    - 尝试将代码发送到服务中的各个位置(onCreate,onStart等)    - 继续恢复活动而不是重新启动它们。    - 以及StackOverflow上提到的关于从服务启动活动,使用状态栏等的所有其他内容。

所以我很难过。如果有人知道如何处理这个,我真的很感激帮助。

编辑:这是完整的堆栈跟踪:

11-01 04:58:21.111: ERROR/Zygote(32): setreuid() failed. errno: 2
11-01 04:58:28.332: ERROR/Zygote(32): setreuid() failed. errno: 17
11-01 04:58:29.412: ERROR/BatteryService(58): usbOnlinePath not found
11-01 04:58:29.412: ERROR/BatteryService(58): batteryVoltagePath not found
11-01 04:58:29.412: ERROR/BatteryService(58): batteryTemperaturePath not found
11-01 04:58:29.431: ERROR/SurfaceFlinger(58): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
11-01 04:58:35.131: ERROR/EventHub(58): could not get driver version for /dev/input/mouse0, Not a typewriter
11-01 04:58:35.131: ERROR/EventHub(58): could not get driver version for /dev/input/mice, Not a typewriter
11-01 04:58:35.382: ERROR/System(58): Failure starting core service
11-01 04:58:35.382: ERROR/System(58): java.lang.SecurityException
11-01 04:58:35.382: ERROR/System(58):     at android.os.BinderProxy.transact(Native Method)
11-01 04:58:35.382: ERROR/System(58):     at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
11-01 04:58:35.382: ERROR/System(58):     at android.os.ServiceManager.addService(ServiceManager.java:72)
11-01 04:58:35.382: ERROR/System(58):     at com.android.server.ServerThread.run(SystemServer.java:184)
11-01 04:58:36.081: ERROR/SoundPool(58): error loading /system/media/audio/ui/Effect_Tick.ogg
11-01 04:58:36.081: ERROR/SoundPool(58): error loading /system/media/audio/ui/KeypressStandard.ogg
11-01 04:58:36.081: ERROR/SoundPool(58): error loading /system/media/audio/ui/KeypressSpacebar.ogg
11-01 04:58:36.091: ERROR/SoundPool(58): error loading /system/media/audio/ui/KeypressDelete.ogg
11-01 04:58:36.091: ERROR/SoundPool(58): error loading /system/media/audio/ui/KeypressReturn.ogg
11-01 04:58:37.572: ERROR/ThrottleService(58): Could not open GPS configuration file /etc/gps.conf
11-01 04:58:38.641: ERROR/logwrapper(147): executing /system/bin/tc failed: No such file or directory
11-01 04:58:38.691: ERROR/logwrapper(148): executing /system/bin/tc failed: No such file or directory
11-01 04:58:38.702: ERROR/logwrapper(149): executing /system/bin/tc failed: No such file or directory
11-01 04:58:46.651: ERROR/HierarchicalStateMachine(58): TetherMaster - unhandledMessage: msg.what=3
11-01 05:00:22.672: ERROR/AndroidRuntime(274): FATAL EXCEPTION: main
11-01 05:00:22.672: ERROR/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.scrumptious.slugmood/org.scrumptious.slugmood.Page1}: java.lang.NullPointerException
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.os.Looper.loop(Looper.java:123)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at java.lang.reflect.Method.invokeNative(Native Method)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at java.lang.reflect.Method.invoke(Method.java:521)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at dalvik.system.NativeStart.main(Native Method)
11-01 05:00:22.672: ERROR/AndroidRuntime(274): Caused by: java.lang.NullPointerException
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at org.scrumptious.slugmood.Page1.onCreate(Page1.java:36)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     ... 11 more
11-01 05:05:15.972: ERROR/AndroidRuntime(286): FATAL EXCEPTION: main
11-01 05:05:15.972: ERROR/AndroidRuntime(286): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.scrumptious.slugmood/org.scrumptious.slugmood.Page1}: java.lang.NullPointerException
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.os.Looper.loop(Looper.java:123)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at java.lang.reflect.Method.invokeNative(Native Method)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at java.lang.reflect.Method.invoke(Method.java:521)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at dalvik.system.NativeStart.main(Native Method)
11-01 05:05:15.972: ERROR/AndroidRuntime(286): Caused by: java.lang.NullPointerException
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at org.scrumptious.slugmood.Page1.onCreate(Page1.java:36)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     ... 11 more

我仍然不完全确定我的问题是什么,但似乎Android无法找到page1。

1 个答案:

答案 0 :(得分:0)

NullPointerException源文件第36行onCreate() org.scrumptious.slugmood.Page1位置Page1.java