[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]android通知读取

上一篇:[备忘]android监听通知内容
下一篇:[备忘]java获取文件夹下文件名的几种写法

添加日期:2024/10/29 9:44:18 快速返回   返回列表 阅读85次


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"))
这东西叫:通知使用权

实际测试发现,每次要手工勾掉,再勾上,才好使。
不知道为啥。
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved