2017-08-01 227 views
0

我创建一个android应用程序来访问通知。到目前为止,我的NotificationListenerService还不能访问通知。它不在下面显示的允许应用程序通知访问列表中。我如何启用它?我的Manifest.xml,MainActivity.java和MyNotificationListener.java有如下Notifcation访问被拒绝 - Android

的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

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

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

我应该在哪里修改?谢谢。 [email protected]

回答

0

就我所知,您不能以编程方式启用通知侦听。在每个设备中,您必须打开设备设置并访问任何想要阅读通知的应用程序。