得到通知在4.1及以上的Andr​​oid [英] getting the Notifications in 4.1 and above in android

查看:168
本文介绍了得到通知在4.1及以上的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得通知在Android中使用辅助服务在4.0以上?

为什么 onAccessibilityEvent(AccessibilityEvent事件)不是要求在通知上述4.0解雇 请给出正确的建议

在此先感谢

  @覆盖
    公共无效onAccessibilityEvent(AccessibilityEvent事件){

        最终诠释EVENTTYPE = event.getEventType();
        的System.out.println(MyServ.onAccessibilityEvent());
        开关(事件类型){
        案例AccessibilityEvent.TYPE_VIEW_FOCUSED:
            Toast.makeText(getApplicationContext(),通知解雇了,
                    Toast.LENGTH_LONG).show();
            打破;
        案例AccessibilityEvent.TYPE_ANNOUNCEMENT:
            Toast.makeText(getApplicationContext(),类型为公告,
                    Toast.LENGTH_LONG).show();
            打破;
        默认:
            打破;
        }

    }
 

onServiceConnected()

  @覆盖
    保护无效onServiceConnected(){
        super.onServiceConnected();
    AccessibilityServiceInfo信息=新AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
        setServiceInfo(信息);
}
 

解决方案

试试这个code:

 私人无效generateNotification(上下文的背景下,字符串消息,
        长时,查询字符串){

    INT图标= R.drawable.icon;
         时长= System.currentTimeMillis的();
    。字符串APPNAME = context.getResources()的getString(R.string.app_name);
    NotificationManager notificationManager =(NotificationManager)上下文
            .getSystemService(Context.NOTIFICATION_SERVICE);
    INT currentapiVersion = android.os.Build.VERSION.SDK_INT;
    通知通知;

    意向意图=新的意图(背景下,MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,
            意图,0);

        如果(currentapiVersion< android.os.Build.VERSION_ codeS.HONEYCOMB){
            通知=新的通知(图标,邮件,时);
            notification.setLatestEventInfo(背景下,应用程序的名字,消息,
                    contentIntent);
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify((INT)时,通知);

        } 其他 {
            NotificationCompat.Builder建设者=新NotificationCompat.Builder(
                    上下文);
            通知= builder.setContentIntent(co​​ntentIntent)
                    .setSmallIcon(图标).setTicker(应用程序名称).setWhen(时)
                    .setAutoCancel(真).setContentTitle(应用程序名称)
                    .setContentText(消息).build();

            notificationManager.notify((INT)时,通知);

        }

}
 

您可以致电 generateNotification()的方法 onAccessibilityEvent()和通知会显示出来。

How to get the notifications in android using Accessibility Service above 4.0?

Why onAccessibilityEvent(AccessibilityEvent event) not calling When Notification fired above 4.0 Please give Proper Suggestion

Thanks in advance

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

    }

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

解决方案

Try this code :

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

        }

}

You can call generateNotification() method in onAccessibilityEvent() and notifications will be displayed.

这篇关于得到通知在4.1及以上的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆