安卓:NotificationListenerService没有再创造 [英] Android: NotificationListenerService wasn't created again

查看:179
本文介绍了安卓:NotificationListenerService没有再创造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序崩溃,因为一些错误的第一次后,NLService创建并再次绑定,但第2次或第3次之后...,该NLService没有创建和约束的多,连复选框

设置>安全>通知使用权

被选中,我该怎么做这种情况呢?

NLService:

 进口android.content.Intent;
进口android.os.IBinder;
进口android.service.notification.NotificationListenerService;
进口android.service.notification.StatusBarNotification;
进口android.util.Log;公共类NLService扩展NotificationListenerService {
  私有String TAG = this.getClass()getSimpleName()。
  @覆盖
  公共无效的onCreate(){
    super.onCreate();
    Log.d(TAG,== ==的onCreate);
  }  @覆盖
  公共无效的onDestroy(){
    super.onDestroy();
    Log.d(TAG,== ==的onDestroy);
  }  @覆盖
  公共无效onNotificationPosted(StatusBarNotification SBN){
    Log.i(TAG,********** onNotificationPosted);
    Log.i(TAG,ID
        + sbn.getId()
        +\\ t的
        + sbn.getNotification()。tickerText
        +\\ t的
        + sbn.getPackageName());
  }  @覆盖
  公共无效onNotificationRemoved(StatusBarNotification SBN){
    Log.i(TAG,********** onNOtificationRemoved);
    Log.i(TAG,ID
        + sbn.getId()
        +\\ t的
        + sbn.getNotification()。tickerText
        +\\ t的
        + sbn.getPackageName());
  }  @覆盖公众诠释onStartCommand(意向意图,诠释旗帜,INT startId){
    Log.d(TAG,== == onStartCommand);
    返回super.onStartCommand(意向,旗帜,startId);
  }  @覆盖公共的IBinder onBind(意向意图){
    Log.d(TAG,== == onBind);
    返回super.onBind(意向);
  }  @覆盖公共无效onRebind(意向意图){
    super.onRebind(意向);
    Log.d(TAG,== == onRebind);
  }  @覆盖公共布尔onUnbind(意向意图){
    Log.d(TAG,== == onUnbind);
    返回super.onUnbind(意向);
  }
}

和在注册的manifest.xml

 <服务机器人:名字=com.kpbird.nlsexample.NLService
         机器人:标签=@字符串/ APP_NAME
         机器人:权限=android.permission.BIND_NOTIFICATION_LISTENER_SERVICE>
        &所述;意图滤光器>
            <作用机器人:名字=android.service.notification.NotificationListenerService/>
        &所述; /意图滤光器>
< /服务>


解决方案

不过有问题?

我在同样的情况,这似乎NLService不会是协作...

作为假的解决方法我已经注意到,如果你检查,并取消设置>安全>通知使用权在调试会话期间,把一个断点在OnCreate(),您可以拦截服务启动时,但还不是很清楚,为什么有时会停止...

另一种可能性是重新启动设备...

尝试也许你会找到一个方法来解决,我还在努力...

结果的更新

或许真的是在服务改变。
这是我的测试工作在多个设备(我已经尝试好几天)。

安装应用程序可以在启用设置的通知,访问和比你可以试试code后。

的build.gradle 文件

 应用插件:'com.android.application安卓{
    compileSdkVersion 23
    buildToolsVersion23.0.2    defaultConfig {
       的applicationIDyour_application_id
       18的minSdkVersion
       targetSdkVersion 23
       版本code 1
      的versionName1.0
}
    buildTypes {
        发布 {
            minifyEnabled假
            proguardFiles getDefaultProguardFile('proguard的-android.txt'),'proguard-rules.pro
        }
    }
}依赖{
    编译文件树(导演:'库',包括:['的* .jar'])
    testCompile'的JUnit:JUnit的:4.12
    编译com.android.support:appcompat-v7:23.1.1
    编译com.android.support:design:23.1.1
}

的Andr​​oidManifest.xml 文件结果在这里我们声明,并与的NotificationListener(我们创建的类的名称)添加服务

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=insert_your_package>    <应用
        机器人:allowBackup =真
        机器人:图标=@的mipmap / ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:supportsRtl =真
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:全速NAME =
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@风格/ AppTheme.NoActionBar>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;        <服务
            机器人:名字=的NotificationListener
            机器人:标签=@字符串/ APP_NAME
            机器人:权限=android.permission.BIND_NOTIFICATION_LISTENER_SERVICE>
            &所述;意图滤光器>
                <作用机器人:名字=android.service.notification.NotificationListenerService/>
            &所述; /意图滤光器>
        < /服务>
    < /用途>< /清单>

AMain.java 文件

 包insert_your_package;进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.os.Bundle;
进口android.support.design.widget.FloatingActionButton;
进口android.support.design.widget.Snackbar;
进口android.support.v7.app.AppCompatActivity;
进口android.support.v7.widget.Toolbar;
进口android.view.View;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.TextView;公共类全速扩展AppCompatActivity {    保护的TextView的TextView;
    保护NotificationReceiver notificationReceiver;    @覆盖    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.a_main);
        工具条工具栏=(栏)findViewById(R.id.toolbar);
        setSupportActionBar(工具栏);        FloatingActionButton FAB =(FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(查看视图){
                Snackbar.make(看来,用自己的行动替换,Snackbar.LENGTH_LONG)
                        .setAction(动作,NULL).show();
            }
        });        TextView的=(的TextView)findViewById(R.id.textview);        notificationReceiver =新NotificationReceiver();        IntentFilter的IntentFilter的=新的IntentFilter();
        intentFilter.addAction(com.dev.name.test);        registerReceiver(notificationReceiver,IntentFilter的);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.menu_main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_settings){
            返回true;
        }        返回super.onOptionsItemSelected(项目);
    }    类NotificationReceiver扩展广播接收器{        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){            字符串TEMP = intent.getStringExtra(notification_event)+\\ n+ textView.getText();            textView.setText(临时);
        }
    }
}

NotificationListener.java 文件

 包insert_your_package;进口android.content.Intent;
进口android.service.notification.NotificationListenerService;
进口android.service.notification.StatusBarNotification;
进口android.util.Log;公共类的NotificationListener扩展NotificationListenerService {    私有String TAG = this.getClass()getSimpleName()。    @覆盖
    公共无效onNotificationPosted(StatusBarNotification SBN){        Log.i(TAG,********** onNotificationPosted);
        Log.i(TAG,ID:+ sbn.getId()+\\ t+ sbn.getNotification()tickerText +\\ t+ sbn.getPackageName());        意图I =新意图(com.dev.name.test);
        i.putExtra(notification_event,sbn.getPackageName()+贴);        sendBroadcast(ⅰ);
    }    @覆盖
    公共无效onNotificationRemoved(StatusBarNotification SBN){        Log.i(TAG,********** onNotificationPosted);
        Log.i(TAG,ID:+ sbn.getId()+\\ t+ sbn.getNotification()tickerText +\\ t+ sbn.getPackageName());        意图I =新意图(com.dev.name.test);
        i.putExtra(notification_event,sbn.getPackageName()+删除);        sendBroadcast(ⅰ);
    }    @覆盖
    公共无效的onDestroy(){        super.onDestroy();
    }
}

你可以看到文本添加的每个通知的发布删除发生这种情况。现在,该服务是否正常工作我们可以做我们想要的。

尝试,并告诉它是否工作。

After the app crashed because of some bugs for the first time, the NLService was created and bound again, but after 2nd time or 3rd time..., the NLService wasn't created and bound any more, even the checkbox in

Settings > Security > Notification access

is checked, how can I do with this situation?

NLService:

import android.content.Intent;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

public class NLService extends NotificationListenerService {
  private String TAG = this.getClass().getSimpleName();
  @Override
  public void onCreate() {
    super.onCreate();
    Log.d(TAG, "==onCreate==");
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "==onDestroy==");
  }

  @Override
  public void onNotificationPosted(StatusBarNotification sbn) {
    Log.i(TAG, "**********  onNotificationPosted");
    Log.i(TAG, "ID :"
        + sbn.getId()
        + "\t"
        + sbn.getNotification().tickerText
        + "\t"
        + sbn.getPackageName());
  }

  @Override
  public void onNotificationRemoved(StatusBarNotification sbn) {
    Log.i(TAG, "********** onNOtificationRemoved");
    Log.i(TAG, "ID :"
        + sbn.getId()
        + "\t"
        + sbn.getNotification().tickerText
        + "\t"
        + sbn.getPackageName());
  }

  @Override public int onStartCommand(Intent intent, int flags, int startId){
    Log.d(TAG, "==onStartCommand==");
    return super.onStartCommand(intent, flags, startId);
  }

  @Override public IBinder onBind(Intent intent) {
    Log.d(TAG, "==onBind==");
    return super.onBind(intent);
  }

  @Override public void onRebind(Intent intent) {
    super.onRebind(intent);
    Log.d(TAG, "==onRebind==");
  }

  @Override public boolean onUnbind(Intent intent) {
    Log.d(TAG, "==onUnbind==");
    return super.onUnbind(intent);
  }
}

and registered in manifest.xml

<service android:name="com.kpbird.nlsexample.NLService"
         android:label="@string/app_name"
         android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
</service>

解决方案

Still got the problem?

I am in the same situation and this NLService seems won't to be collaborative...

As a "fake" workaround I have notice that if you check and uncheck the Settings > Security > Notification access during a debug session and put a breakpoint in the onCreate() you can intercept when the service starts but is not so clear why sometimes stops...

Another possibility is to reboot the device...

try maybe you'll find a way to solve, I'm still trying...


UPDATE

Maybe something is changed in the service. This is my test that is working on multiple devices (I've try it for days).

After install the app you can enabling the notification access in settings and than you can try the code.

build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
       applicationId "your_application_id"
       minSdkVersion 18
       targetSdkVersion 23
       versionCode 1
      versionName "1.0"
}
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

AndroidManifest.xml file
Here we declare and add the service with NotificationListener (name of the class that we create)

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".AMain"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

</manifest>

AMain.java file

package insert_your_package;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class AMain extends AppCompatActivity {

    protected TextView textView;
    protected NotificationReceiver notificationReceiver;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        textView = (TextView) findViewById(R.id.textview);

        notificationReceiver = new NotificationReceiver();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.dev.name.test");

        registerReceiver(notificationReceiver, intentFilter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class NotificationReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            String temp = intent.getStringExtra("notification_event") + "\n" + textView.getText();

            textView.setText(temp);
        }
    }
}

NotificationListener.java file

package insert_your_package;

import android.content.Intent;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

public class NotificationListener extends NotificationListenerService {

    private String TAG = this.getClass().getSimpleName();

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {

        Log.i(TAG, "**********  onNotificationPosted");
        Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());

        Intent i = new Intent("com.dev.name.test");
        i.putExtra("notification_event", sbn.getPackageName() + " posted");

        sendBroadcast(i);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {

        Log.i(TAG, "**********  onNotificationPosted");
        Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());

        Intent i = new Intent("com.dev.name.test");
        i.putExtra("notification_event", sbn.getPackageName() + " removed");

        sendBroadcast(i);
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }
}

as you can see the text add every notification posted or removed that happens. Now that the service is working properly we can make what we want.

Try and tell if it works.

这篇关于安卓:NotificationListenerService没有再创造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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