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

BroadcastReceiver에서 Notification을 만들었는데 문제가 있어요 [closed]

0 추천
package bst.personal.project.BlueKnight;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class Receiver extends BroadcastReceiver {
    Context mContext;
    static SharePref prefs = null;

    @Override
    public void onReceive(final Context context, Intent intent) {
        mContext = context;
        String action = intent.getAction();

        if (action.equals("noti.test")) {
            Toast.makeText(context, "알림!", Toast.LENGTH_SHORT).show();
            int nt = prefs.getValue("nt", 1);
            if (nt != 1) {
                nt = 1;
            } else {
                nt = 0;
            }
            prefs.put("nt", nt);
            NotiHandler.sendEmptyMessage(nt);
        }
        else if (action.equals("com.example.bst")) {
            Log.e("com.example.bst", "call");
            Intent i = new Intent(mContext, MainActivity.class);
            mContext.startActivity(i);
        }
    }

    Handler NotiHandler = new Handler() {
        public void handleMessage(Message msg) {
            NotificationManager NotiManager = (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE); //알람매니저 생성
            Notification.Builder builder = new Notification.Builder(mContext);
            builder.setContentTitle("제목");
            builder.setContentText("내용");
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setAutoCancel(false);
            Notification noti = builder.build();
            noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;

            Intent nIntent = new Intent("com.example.bst");
            PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, nIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews contentview = new RemoteViews(mContext.getPackageName(), R.layout.remoteview);
            contentview.setOnClickPendingIntent(R.id.btn, pIntent);
            noti.contentView = contentview;

            switch (msg.what) {
                case 0:
                    NotiManager.notify(1, noti);
                    break;
                case 1:
                    NotiManager.cancel(1);
                    break;
            }
        }
    };
}

이렇게 만들었습니다. 브로드캐스트리시버에서 "noti.test"방송을 받으면 핸들러를 통해

노티피케이션을 만듭니다. 그러면 RemoteView를 통해 넣은 버튼이 있죠.

이 버튼을 누르면 "com.example.bst"방송을 보내고 로그를 보여주고 메인액티비티를 실행하게 했는데

로그를 보여주지도 않고 메인액티비티도 실행이 안되네요.

질문을 종료한 이유: 해결
Bateaux (5,200 포인트) 님이 2016년 6월 29일 질문
Bateaux님이 2016년 6월 30일 closed
...