2017-08-01 147 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

就我所知,您不能以編程方式啓用通知偵聽。在每個設備中,您必須打開設備設置並訪問任何想要閱讀通知的應用程序。