显示通知,广播接收器 [英] Showing notification in broadcast receiver

查看:157
本文介绍了显示通知,广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,
我在我需要的时候呼叫到达显示通知的情况。对于这个我使用的广播接收机。 code是低于

I am in a situation that I need to display notification when call arrives. For this I am using broadcast receiver. Code is below

public class MyPhoneStateListener extends PhoneStateListener {
    public Context context;
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        // TODO Auto-generated method stub
        super.onCallStateChanged(state, incomingNumber);
        switch(state){
        case TelephonyManager.CALL_STATE_IDLE:
            NotificationManager notifier = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

            //Get the icon for the notification
            int icon = R.drawable.icon;
            Notification notification = new Notification(icon,"Simple Notification",System.currentTimeMillis());

            //Setup the Intent to open this Activity when clicked
            Intent toLaunch = new Intent(context, main.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, toLaunch, 0);

            //Set the Notification Info
            notification.setLatestEventInfo(context, "Hi!!", "This is a simple notification", contentIntent);

            //Setting Notification Flags
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.flags |= Notification.FLAG_INSISTENT;
          //  notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
            //Send the notification
            notifier.notify(0x007, notification);
            Log.d("CALL", "IDLE");
        break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
          Log.d("DEBUG", "OFFHOOK");
        break;
        case TelephonyManager.CALL_STATE_RINGING:

          Log.d("DEBUG", "RINGING");
        break;
        }
    }

}

现在的问题是,声音的通知没有打,但被正确显示的通知。任何人都可以阐明它请为什么通知声音没有被打?

Now problem is that sound for the notification is not playing but notification is being displayed correctly. Can anyone shed light on it please why notification sound is not being played?

推荐答案

我最终通过媒体播放器手动播放声音。 code现在看起来像

I ended up playing sound manually via media player. Code Now Looks like

public class MyPhoneStateListener extends PhoneStateListener {
    public Context context;
    public static final String PREFS_NAME = "ealertprefs";
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        // TODO Auto-generated method stub      
        switch(state){
        case TelephonyManager.CALL_STATE_IDLE:                     
            Log.v("CALL", "IDLE");
        break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
          Log.d("DEBUG", "OFFHOOK");
        break;
        case TelephonyManager.CALL_STATE_RINGING:
          if(call_from_elist(incomingNumber)) {
              wake_up_phone();
              send_notification();
              create_sound();
          }
          Log.d("DEBUG", "RINGING");
        break;
        }
    }
    private boolean call_from_elist(String number) {
        return true;
    }
    private void wake_up_phone() {
        AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    }
    private void send_notification(){
        NotificationManager notifier = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);                
        int icon = R.drawable.icon;
        Notification notification = new Notification(icon,"Simple Notification",System.currentTimeMillis());        
        Intent toLaunch = new Intent(context, main.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, toLaunch, 0);        
        notification.setLatestEventInfo(context, "Hi!!", "This is a simple notification", contentIntent);        
        notification.flags |= Notification.FLAG_AUTO_CANCEL;                              
        notifier.notify(0x007, notification);
    }
    private void create_sound() {
        Uri alert;
        SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
        String ringtone = settings.getString("ringtone_uri", "");
        if(ringtone.equals("")) {
            alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
        else {
            alert = Uri.parse(ringtone);
        }

        MediaPlayer mMediaPlayer = new MediaPlayer();
        try {
            mMediaPlayer.setDataSource(context, alert);
            //final AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
            //if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) != 0) {
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
                //mMediaPlayer.setLooping(true);
                mMediaPlayer.prepare();
                mMediaPlayer.start();
            //}
        }
        catch(IOException e) {
            Log.v("CALL", e.getMessage());
        }
    }
}

这篇关于显示通知,广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆