package com.example.mynotify;
import android.app.Notification; import android.content.ComponentName; import android.content.pm.PackageManager; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Vibrator; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification;
import java.util.Calendar;
public class MyNotificationListenerService extends NotificationListenerService {
//private MediaPlayer mediaPlayer; private Vibrator vibrator;
private Ringtone ringtone;
@Override public void onCreate() { super.onCreate(); //mediaPlayer = MediaPlayer.create(this, R.raw.your_music); }
@Override public void onNotificationPosted(StatusBarNotification sbn) { super.onNotificationPosted(sbn);
CharSequence text = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT); if(text!=null){ System.out.println(text); if (text.toString().contains("新增订单")) { Calendar calendar = Calendar.getInstance(); int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
if (currentHour >= 10 && currentHour < 15) { //mediaPlayer.start(); }else{ // 获取Vibrator实例 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
// 振动手机,传入参数为振动的毫秒数 vibrator.vibrate(2000); // 振动1秒钟
// 如果需要重复振动,可以使用以下方法 // vibrator.vibrate(new long[]{100, 1000, 1000, 1000}, -1); // 振动1秒,停止100毫秒,再振动1秒,循环下去
// 获取默认电话铃声的Uri Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
// 创建Ringtone实例 ringtone = RingtoneManager.getRingtone(getApplicationContext(), defaultRingtoneUri);
// 播放电话铃声 ringtone.play(); } } }
}
@Override public void onDestroy() { super.onDestroy(); // 停止振动 if (vibrator != null) { vibrator.cancel(); }
// 停止播放电话铃声 if (ringtone != null && ringtone.isPlaying()) { ringtone.stop(); } //mediaPlayer.release(); }
@Override public void onListenerDisconnected(){ requestRebind(new ComponentName(this,MyNotificationListenerService.class)); }
@Override public void onListenerConnected() { super.onListenerConnected(); tryReconnectService(); }
public void tryReconnectService() { toggleNotificationListenerService(); requestRebind(new ComponentName(this,MyNotificationListenerService.class)); }
/** * Try deactivate/activate your component service */ private void toggleNotificationListenerService() { PackageManager pm = getPackageManager(); pm.setComponentEnabledSetting(new ComponentName(this, MyNotificationListenerService.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); pm.setComponentEnabledSetting(new ComponentName(this, MyNotificationListenerService.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } }
xml增加: <uses-permission android:name="android.permission.VIBRATE" /> <service android:name="com.example.mynotify.MyNotificationListenerService" android:exported="true" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service> MainActivity的onCreate里,打开设置界面 startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")) 这东西叫:通知使用权
实际测试发现,每次要手工勾掉,再勾上,才好使。 不知道为啥。
|