2017-03-28 4 views
0

내 앱이 백그라운드에서 실행 중일 때 알림이 알림 바에 표시되는 방법을 구현했습니다. 알림을 클릭하면 앱을 닫습니다.android 알림을 클릭 한 후 애플리케이션 종료

Intent intent = new Intent(this, Main.class); 
PendingIntent pIntent = PendingIntent.getActivity(this, 
          (int) System.currentTimeMillis(), intent, 0); 
if (Build.VERSION.SDK_INT < 16) { 
    Notification n = new Notification.Builder(this) 
      .setContentTitle("Flashlight") 
      .setContentText("turn off") 
      .setSmallIcon(R.mipmap.light_on) 
      .setContentIntent(pIntent) 
      .setAutoCancel(true).getNotification(); 
    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(0, n); 
} else { 
    Notification n = new Notification.Builder(this) 
      .setContentTitle("Flashlight") 
      .setContentText("turn off") 
      .setSmallIcon(R.mipmap.light_on) 
      .setContentIntent(pIntent) 
      .setAutoCancel(true).build(); 
    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(0, n); 
} 

어떻게하면됩니까?

+0

"close app"이란 정확히 무엇을 의미합니까? –

+0

@MikeM. 스택에 다른 활동이 없습니다. 백그라운드에서 실행중인 유일한 활동입니다. 클릭 후 내 성화를 끄고 – DastakWall

답변

1

당신이 다음, 당신의 의도에 대한 작업을 설정 방송 수신기를 만들고 내부 응용 프로그램을 종료 할 수 있습니다 응용 프로그램을 종료해야하는 경우 onReceive 메소드를 호출 한 다음 IntentFilter를 사용하여 액션을 수신하고 수신기에 액션 필터를 등록하십시오. 당신의에서 onCreate에서 다음

Intent intent = new Intent("close_app"); 
PendingIntent pIntent = PendingIntent.getBroadcast(this, (int) 
       System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT); 
. 
. 
.// build your notification 

:

private BroadcastReceiver mReceiver; 

    mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("TAG" ,"onReceive "); 
     finish(); 
     } 
    }; 

그런 다음 조치를받을과에서 onCreate 나 onResume에 등록 :

 IntentFilter filter = new IntentFilter(); 
     filter.addAction("close_app"); 
     registerReceiver(mReceiver, filter); 

을 그리고 등록을 취소하는 것을 잊지 마세요 다음은 예입니다 귀하의 리시버

0

애플리케이션 또는 활동을 종료 하시겠습니까?

일반적으로 활동을 오랫동안 사용하지 않으면 재활용됩니다.

응용 프로그램을 죽이고 싶은 경우에, 당신은이를 사용할 수 있습니다

Process.killProcess(Process.myPid()) 
+0

을 종료하고 내 프로세스 ID를 얻는 방법은 무엇입니까? – Jorgesys

관련 문제