【问题标题】:getting the Notifications in 4.1 and above in android在 android 中获取 4.1 及更高版本的通知
【发布时间】:2014-12-19 21:28:19
【问题描述】:

如何使用4.0以上的Accessibility Service在android中获取通知?

为什么onAccessibilityEvent(AccessibilityEvent event) 在 4.0 以上触发通知时不调用 请给出正确的建议

提前致谢

@Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

        final int eventType = event.getEventType();
        System.out.println("MyServ.onAccessibilityEvent()");
        switch (eventType) {
        case AccessibilityEvent.TYPE_VIEW_FOCUSED:
            Toast.makeText(getApplicationContext(), "Notification fired",
                    Toast.LENGTH_LONG).show();
            break;
        case AccessibilityEvent.TYPE_ANNOUNCEMENT:
            Toast.makeText(getApplicationContext(), "Type is Announcement",
                    Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }

    }

onServiceConnected()

@Override
    protected void onServiceConnected() {
        super.onServiceConnected();
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
        setServiceInfo(info);
}

【问题讨论】:

  • 它正在工作,谢谢@androiduser
  • 调用Broadcast不止一次??我怎样才能控制@androiduser
  • 调用Broadcast不止一次??这意味着什么??我猜你是想说你的广播接收器被多次调用......
  • 是的,当我从可访问性事件中获取事件时,我想调用 Broadcast Receiver @androiduser

标签: android android-service


【解决方案1】:

试试这个代码:

    private void generateNotification(Context context, String message,
        long when, String query) {

    int icon = R.drawable.icon;
         long when = System.currentTimeMillis();
    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Notification notification;

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            intent, 0);

        if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
            notification = new Notification(icon, message, when);
            notification.setLatestEventInfo(context, appname, message,
                    contentIntent);
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify((int) when, notification);

        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    context);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(appname).setWhen(when)
                    .setAutoCancel(true).setContentTitle(appname)
                    .setContentText(message).build();

            notificationManager.notify((int) when, notification);

        }

}

你可以在onAccessibilityEvent()中调用generateNotification()方法,会显示通知。

【讨论】:

  • 您的答案工作正常,但是当我收到任何消息或电话时,通知状态栏中会显示多个通知。它应该显示每个事件@androiduser 的单个通知
  • 对于单个通知,您应该使用:notificationManager.notify(0, notification);而不是 notificationManager.notify((int) when, notification);
猜你喜欢
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 2021-04-20
  • 2020-03-09
  • 1970-01-01
  • 2018-09-29
相关资源
最近更新 更多