简体   繁体   English

通知访问被拒绝-Android

[英]Notifcation Access Denied - Android

Im creating an android app to access notifications. 我创建了一个Android应用来访问通知。 So far my NotificationListenerService cant access the notifications yet. 到目前为止,我的NotificationListenerService还无法访问通知。 It isnt on the list of allow apps notification access shown below. 它不在下面显示的允许应用程序通知访问列表中。 How may I enable that? 我该如何启用? My Manifest.xml, MainActivity.java and MyNotificationListener.java are as below 我的Manifest.xml,MainActivity.java和MyNotificationListener.java如下

Manifest.xml 的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="me.shikanga.NotificationListen2" >

    <uses-permission android:name="android.permission.WRITE_EXRERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:resizeableActivity = "true">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyNotificationListener"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
            >
            <intent-filter>
                <action android:name="android.service.notification.MyNotificationListener"/>
            </intent-filter>
        </service>
    </application>

</manifest>

MyNotificationListener.java MyNotificationListener.java

package me.shikanga.NotificationListen2;

import android.service.notification.NotificationListenerService;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.util.*;
import android.app.*;
import android.service.notification.NotificationListenerService.*;
import android.service.notification.*;
import java.io.*;
import java.util.*;
import android.graphics.*;

public class MyNotificationListener extends NotificationListenerService
{
    @Override
    public void onNotificationPosted(StatusBarNotification sbn, NotificationListenerService.RankingMap rankingMap)
    {
        // TODO: Implement this method
        super.onNotificationPosted(sbn, rankingMap);

    String pack = sbn.getPackageName();
    String ticker="";
    if(sbn.getNotification().tickerText.toString()!=null)
    {
        ticker=sbn.getNotification().tickerText.toString();
    }
    Bundle extras=sbn.getNotification().extras;
    String title=extras.getString("android.title");
    String text= extras.getCharSequence("android.text").toString();
    int id1= extras.getInt(Notification.EXTRA_SMALL_ICON);
    Bitmap id = sbn.getNotification().largeIcon;

    Toast.makeText(getApplicationContext(),pack +title+text, Toast.LENGTH_SHORT).show();
    Log.e("Posted", pack+ticker+title+text);
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn)
{
    // TODO: Implement this method
    super.onNotificationRemoved(sbn);

    String pack = sbn.getPackageName();
    Log.e("Removed", pack);
}
}

MainActivity.java MainActivity.java

package me.shikanga.NotificationListen2;

import android.app.*;
import android.os.*;
import android.widget.Button;
import android.view.View.*;
import android.view.*;
import android.content.*;
import android.widget.*;
import android.util.*;
import java.util.*;
import android.provider.*;

public class MainActivity extends Activity 
{
    Button startButton,stopButton;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    startButton=(Button) findViewById(R.id.startButton);
    stopButton=(Button) findViewById(R.id.stopButton);
    context=getApplicationContext();

    if(!checkNotificationEnabled())
    {
        Toast.makeText(context, "Notification access denied", Toast.LENGTH_SHORT).show();
        Log.e(getPackageName(), "Notification Access denied");
    }
    else
    {
        Toast.makeText(context, "Notification access enabled", Toast.LENGTH_SHORT).show();
        Log.e(getPackageName(), "Notification Access Enabled");

        //go to settimgs to enable notification access
        //startActivity(new Intent(Settings.ACTION_SECURITY_SETTINGS));
    }

    startButton.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            try
            {
                Intent i = new Intent(getApplicationContext(), MyNotificationListener.class);
                startService(i);

                Date date= new Date();
                Log.e(getPackageName()+" "+date.toString(),"MyNotificationListener service intent called sucessfully");
                //Toast.makeText(getApplicationContext(), date.toString()+"Intent called successfully", Toast.LENGTH_SHORT).show();
            }
            catch(Exception e)
            {
                //Toast.makeText(getApplicationContext(), "Failed to call intent", Toast.LENGTH_SHORT).show();
                Date date = new Date();
                Log.e(getPackageName()+" "+date.toString(),"Failed to start MyNotificationListener Service");
            }
        }
    });

    stopButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                try
                {
                    Intent i = new Intent(getApplicationContext(), MyNotificationListener.class);
                    stopService(i);
                    Date date = new Date();
                    Log.e(getPackageName()+" "+date.toString(),"MyNotificationListener service stop intent called sucessfully");
                    //Toast.makeText(getApplicationContext(), "Intent called successfully", Toast.LENGTH_SHORT).show();
                }
                catch(Exception e)
                {
                    //Toast.makeText(getApplicationContext(), "Failed to call intent", Toast.LENGTH_SHORT).show();
                    Date date = new Date();
                    Log.e(getPackageName()+" "+date.toString(),"Failed to start MyNotificationListener Service");
                }
            }
        });
}


//Check of notification acccess is enabled
public boolean checkNotificationEnabled()
{
    String enabledListeners = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");
    String packageName= getPackageName();
    if (enabledListeners.contains(packageName))
    {
        return true;
    }
    else
        {
            return false;
        }
}
}

Where should I modify? 我应该在哪里修改? Thanks. 谢谢。 jerryshikanga@gmail.com jerryshikanga@gmail.com

As far as i know you can't enable the notification listening programmatically. 据我所知,您无法以编程方式启用通知监听。 In every device, you have to open the device settings and give access to any single app that want to read the notifications. 在每台设备中,您都必须打开设备设置,并授予要阅读通知的任何单个应用程序的访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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