마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

notification 클릭시 실행된 액티비티 띄우기

0 추천

 

안녕하세요 안드로이드 초보자입니다.

메인 액티비티 버튼을 실행하면 서비스를 실행시키면서 알림창에 알림이 뜨도록 했는데요

여기서 알림창을 클릭하면 다시 메인 액티비티가 뜨도록 하고 싶은데 아무 반응이 없어요

서비스 실행되는 곳에 PendingIntent 를 넣었는데 왜 안되는지 잘 모르겠네요;; 설명 주시면 감사하겠습니다.

 

public int onStartCommand(Intent aIntent, int aFlags, int aStartId) {
        super.onStartCommand(aIntent, aFlags, aStartId);
 
        Log.i("test", "onStartCommand()");
 
        Notification notification = new Notification(R.drawable.ic_launcher, "서비스 실행됨", System.currentTimeMillis());
        notification.setLatestEventInfo(getApplicationContext(), "Screen Service", "서비스 실행중", null);
 
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
 
        startForeground(1, notification);
 
        sm.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_NORMAL);
 
        return START_STICKY;
    }
 
 
 
파워백곰 (770 포인트) 님이 2015년 5월 11일 질문

2개의 답변

0 추천
 
채택된 답변
startForeground 위에 NotificationManager 객체 할당하고

notificationManager.notify(1, notification) 을 추가해보세여

 

참고로 위 방식으로 Notification 생성하는 방법은 deprecated 되었으므로

Notification.Builder를 사용하기를 권장합니다.
Gradler (109,780 포인트) 님이 2015년 5월 11일 답변
파워백곰님이 2015년 5월 11일 채택됨
0 추천
@Override
 public int onStartCommand(Intent intent, int flags, int startId) {

 Intent notiintent = new Intent(this, MainActivity.class);
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notiintent, 0);
 Notification notification = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setWhen(System.currentTimeMillis())
    .setContentTitle("---")
    .setContentText("-----")   

.setContentIntent(pendingIntent)            // 앱 실행 버튼
    .build();
   
  startForeground(NOTIFICATION_ID, notification);
쿠쿠부다스 (6,470 포인트) 님이 2016년 1월 19일 답변
...