Android - 通知,设备没有醒来,没有声音或振动

时间:2014-08-24 15:32:06

标签: android notifications xamarin.android

我正在尝试为我的应用创建通知,唤醒设备,播放声音并振动。以下是我正在使用的代码。我在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);

1 个答案:

答案 0 :(得分:0)

我在构建的应用中遇到类似的情况。我想唤醒手机并关闭任何键盘锁。我99%肯定这不是可行的方法,但可以帮助我们俩。下面的代码忠实地打开设备并按照我的意愿关闭键盘锁并播放n闹钟。但是,它往往会使手机处于永不允许手机重新进入睡眠状态的状态,后续闹钟只会唤醒设备,但拒绝播放任何警报声。

这是服务

   @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());
        }
    }

}