简体   繁体   中英

Android - notification, device not waking up, no sound or vibration

I am trying to create a notification for my app that wakes up the device, plays a sound and vibrates. Following is the code that I am using. I found several such question in SO and tried the code in them. But the notification is still not waking up the device, there is no sound or vibration. The notification is showing up alright. Can anyone help me figure out what is wrong with the code?

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);

I have a similar situation on an app I'm building. I want to wake up the phone and turn off any keyguard. I'm 99% certain this is NOT the way to go, but may help us both. The code below faithfully turns on the device and turns off the keyguard as I wanted and plays n alarm. However, it tends to leave the phone in a state of never allowing the phone to put the screen back to sleep and subsequent alarms merely wake the device but refuse to play any alarm sound.

this is the service

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

   }

this is the activity

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM