Android通知 - 在Flashlight应用

时间:2016-05-11 13:44:51

标签: android notifications android-camera resume flashlight

我有一个简单的手电筒应用程序的问题。 我已经集成了PARTIAL_WAKE_LOCK功能,现在用户可以在带有led / on运行的应用程序时按下主页或电源按钮。因此,为了更直观,我想在Android栏上集成通知系统,当用户点击Notification时可以返回正在运行的活动,但是点击通知时出现问题意图转到新活动并尝试获取相机参数。导致应用程序崩溃并重新启动。

我的通知代码:

@Override
protected void onStop() {

    super.onStop();
    wakeLock.acquire();

    if (isFlashlightOn) {
        nmgr =      (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        noti = new Notification(R.drawable.small_ico,"LED/ON", System.currentTimeMillis());
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.setAction(Intent.ACTION_MAIN);
        resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        PendingIntent PIntent = PendingIntent.getActivity(this, 0,
                resultIntent, PendingIntent.FLAG_UPDATE_CURRENT );

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
        noti.setLatestEventInfo(this, "FLASHLIGHT", "LED / ON", pIntent);
        nmgr.notify(NOTIFICATION_ID,noti);

    } else {
        nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        noti = new Notification(R.drawable.small_ico,"LED/OFF", System.currentTimeMillis());
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.setAction(Intent.ACTION_MAIN);
        resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        PendingIntent PIntent = PendingIntent.getActivity(this, 0,
                resultIntent, PendingIntent.FLAG_UPDATE_CURRENT );

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
        noti.setLatestEventInfo(this, "FLASHLIGHT", "LED / OFF", pIntent);
        nmgr.notify(NOTIFICATION_ID,noti);
    }
    int mod=myAudioManager.getRingerMode();

}

这是关于Pause,Resume ecc。

的代码
 @Override
public void onPause() {
    super.onPause();
    int mod=myAudioManager.getRingerMode();
    if (mAdView != null) {
        mAdView.pause();
    }
  }

@Override
public void onResume() {
    super.onResume();
    if (mAdView != null) {
        mAdView.resume();
    }
}

@Override
public void onDestroy() {
    int mod=myAudioManager.getRingerMode();
    if (mAdView != null) {
        mAdView.destroy();
    }
    super.onDestroy();
      }

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    GoogleAnalytics.getInstance(this).reportActivityStart(this);
}

@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    if (isFlashlightOn) {
    } else {
      setFlashlightOn();
    }
    GoogleAnalytics.getInstance(this).reportActivityStart(this);
}

我的问题是,当点击通知时,如何恢复正在运行的活动?

崩溃时出错:

FATAL EXCEPTION: main
Process: com.flashlight, PID: 16164
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flashlight/com.flashlight.MainActivity}: 
java.lang.RuntimeException: Fail to connect to camera service
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:469)
at android.hardware.Camera.open(Camera.java:442)
at com.flashlight.MainActivity.onCreate(MainActivity.java:292)
at android.app.Activity.performCreate(Activity.java:5305)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5292) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
at dalvik.system.NativeStart.main(Native Method) 

这很奇怪,因为问题只发生在第一次刚安装应用程序时,关闭并重新打开后才能完美运行。

似乎是因为已经采取了摄像机参数,因此点击通知的意图正在进行其他应用程序进程并崩溃。

快捷方式可能有问题吗?

请帮帮我 谢谢大家

0 个答案:

没有答案