Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android通知侦听器服务自行启动_Java_Android_Service - Fatal编程技术网

Java Android通知侦听器服务自行启动

Java Android通知侦听器服务自行启动,java,android,service,Java,Android,Service,我正在尝试制作一个能够获取所有通知并完成任务的应用程序。但我的问题是,即使我没有调用启动服务,NotificationListener服务也会自动启动。只要我允许手机上的应用程序通知访问,它就会启动。所以服务不知怎么开始了,我无法阻止它。我已经尝试过简单的服务,它们工作正常。但是这个有NotificationListener的真的很痛苦。我只想通过我的命令启动和停止此服务 当我允许时,服务会自动启动(屏幕截图) 主要活动 MyService类 AndroidManifest.xml >

我正在尝试制作一个能够获取所有通知并完成任务的应用程序。但我的问题是,即使我没有调用启动服务,NotificationListener服务也会自动启动。只要我允许手机上的应用程序通知访问,它就会启动。所以服务不知怎么开始了,我无法阻止它。我已经尝试过简单的服务,它们工作正常。但是这个有NotificationListener的真的很痛苦。我只想通过我的命令启动和停止此服务

当我允许时,服务会自动启动(屏幕截图)

主要活动 MyService类 AndroidManifest.xml

>

棘手的部分在清单中。当我删除意图过滤器部分并运行应用程序时。它不想再访问通知,也不想自己启动。我可以使用按钮从主活动启动和停止服务。但这次应用程序没有收到通知

            // just delete this lines
            <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
//删除这行就行了

这是一种预期行为

当您授予通知访问权限(或重新启动手机)时,系统将绑定到您的
NotificationListenerService
,以便发送通知数据


如果你重写你的服务
onBind
方法并记录它,你会看到它正在被调用。

那么当我设置onBind方法时,它不会自动启动吗?因为我真正的问题是我无法控制的工作@Pawel@nerepl不,不允许您阻止通话。系统只调用它一次,不重试,在这种情况下,您的服务将不会收到通知回调。然后,我无法使用NotificationListenerService按钮启动或停止,所以我只需与活动通信,并让服务一直运行,对吗@Pawel@nerepl是的,它需要保持活动状态,您所能做的最好的事情是让您的按钮切换共享首选项,并在通知服务中检查它的值,以查看它是否应该忽略/处理通知。
package com.example.alperen.nservice2;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

public class MyService extends NotificationListenerService{

@Override
public void onCreate() {
    super.onCreate();
    System.out.println("********* SERVICE STARTED ***********");
}

@Override
public void onNotificationPosted(StatusBarNotification sbn) {

    System.out.println("***** notification *****");
    String pack,title,text;
    Bundle extras;

    try {
        pack = sbn.getPackageName();
        extras = sbn.getNotification().extras;
        title = extras.getString("android.title").toString();
        text = extras.getCharSequence("android.text").toString();
    }catch (Exception e)
    {
        System.out.println("**** HATA NOTIFYSERVICE CLASS ****");
        pack="empty1";
        title="empty1";
        text="empty1";
        System.out.println("**** "+pack+" ****");
    }

    Log.i("Package",pack);
    Log.i("Title",title);
    Log.i("Text",text);

    Toast.makeText(this,"title: "+title+" text: "+text,Toast.LENGTH_LONG).show();
}

@Override
public void onDestroy() {
    System.out.println("***** destroyed *****");
    super.onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.alperen.nservice2">

<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
    tools:ignore="ProtectedPermissions" />

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

    <service android:name=".MyService"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
        ></service>

</application>
            // just delete this lines
            <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>