【问题标题】:Android - notification, device not waking up, no sound or vibrationAndroid - 通知、设备未唤醒、没有声音或振动
【发布时间】:2014-08-24 23:50:15
【问题描述】:

我正在尝试为我的应用创建一个通知,用于唤醒设备、播放声音和振动。以下是我正在使用的代码。我在 SO 中发现了几个这样的问题并尝试了其中的代码。但是通知仍然没有唤醒设备,没有声音或振动。通知显示正常。谁能帮我弄清楚代码有什么问题?

Android.Net.Uri alarmSound = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .SetAutoCancel(true) 
            .SetContentIntent(resultPendingIntent) 
            .SetContentTitle("Title")
            .SetSmallIcon(Resource.Drawable.Icon) 
            .SetContentText(String.Format("Message!"));
        Notification notification =builder.Build();
        notification.Sound = alarmSound;
        notification.Defaults = NotificationDefaults.Sound| NotificationDefaults.Vibrate | NotificationDefaults.Lights;
        NotificationManager nManager = (NotificationManager)GetSystemService(Context.NotificationService);
        nManager.Notify(0, notification);

【问题讨论】:

    标签: android notifications xamarin.android


    【解决方案1】:

    我正在构建的应用程序也遇到了类似的情况。我想唤醒手机并关闭任何键盘保护。我有 99% 的把握这不是要走的路,但可能对我们双方都有帮助。下面的代码忠实地打开设备并按照我的意愿关闭键盘保护并播放警报。但是,它往往会使手机处于永远不允许手机重新进入睡眠状态的状态,随后的警报只会唤醒设备而拒绝播放任何警报声音。

    这是服务

       @Override
       public void onStart(Intent intent, int startId) {
          PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
          WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
          wakeLock.acquire();
    
          KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); 
          KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock("TAG");
          keyguardLock.disableKeyguard();
    
          Bundle bundle = intent.getExtras();
          String why = bundle.getString("WHY");
          Log.d("RemindMe","Wake the hell up "+why);
    
          Intent i = new Intent(getApplicationContext(), WakeUpShowReminder.class);
          i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          i.putExtras(bundle);
          startActivity(i);
    
       }
    

    这是活动

    public class WakeUpShowReminder extends Activity {
    
    
        private MediaPlayer mMediaPlayer;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_reminder);
    
        Button stop = (Button) findViewById(R.id.stop_alarm);
        stop.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                stopPlaySound();
                finish();
    
            }
        });
    
        String why = "";
        String sound;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            why = extras.getString("WHY");
            sound = extras.getString("MEDIA");
        }
        else {
            why = "WTF";
            sound="content://media/internal/audio/media/44";
        }
    
        TextView reason = (TextView) findViewById(R.id.reason);
        reason.setText(why);
    
    
        playSound(this, Uri.parse(sound));
    }
    
    
    @Override
    protected void onPause() {
        super.onPause();
        mMediaPlayer.stop();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mMediaPlayer.stop();
    }
    
    private void stopPlaySound() {
        mMediaPlayer.stop();
    }
    
    private void playSound(Context context, Uri alert) {
            mMediaPlayer = new MediaPlayer();
            try {
                mMediaPlayer.setDataSource(context, alert);
                final AudioManager audioManager = (AudioManager) context
                        .getSystemService(context.AUDIO_SERVICE);
                if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
                    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                }
            } catch (IOException e) {
                System.out.println("OOPS");
                Log.d("REMINDME","Media Error "+e.toString());
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多