我正在尝试在Android中实现NotificationListenerService
,但尚未启动。
MyNotificationService
public class MyNotificationService extends NotificationListenerService {
private static final String TAG = "MyNotificationService";
private boolean mConnected = false;
@Override
public ComponentName startService(Intent service) {
Log.d(TAG, "Starting Notification service");
return super.startService(service);
}
@Override
public void onListenerConnected() {
super.onListenerConnected();
Log.d(TAG, "Listener connected");
mConnected = true;
}
@Override
public void onListenerDisconnected() {
super.onListenerDisconnected();
Log.d(TAG, "Listener disconnected");
mConnected = false;
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.d(TAG, "Listener notification posted mConnected=" + mConnected);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Log.d(TAG, "Listener notification posted mConnected=" + mConnected);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private static final String CHANNEL_ID = "mychannel";
private static final String TAG = "NotificationService:MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "on create");
createNotificationChannel();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.id.icon)
.setContentTitle("contenttitle")
.setContentText("contenttext")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.notify(1001, mBuilder.build());
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "received " + intent.getAction());
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
}
}, new IntentFilter("android.service.notification.NotificationListenerService"));
}
private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "mychannelname", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("mychanneldescription");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fxnq76.notificationservice">
<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=".MyNotificationService"
android:label="MyNotificationServiceLabel"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>
</manifest>
唯一打印的日志是创建时的日志。似乎意图甚至没有得到传达。如您所见,我正在尝试在MainActivity中接收并打印它,但是该语句从不打印。
我尝试用adb shell am start -a "android.service.notification.NotificationListenerService"
手动发送意图,但是我得到了
开始:意图{ act = android.service.notification.NotificationListenerService}错误: 活动尚未开始,无法解决意图{ act = android.service.notification.NotificationListenerService flg = 0x10000000}
我已经阅读了用户需要手动启用通知权限的信息,但是当我进入设置应用程序时,它并没有要求任何与通知相关的权限。我尝试在清单中添加<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
,但没有任何区别。
如何使此通知侦听器服务运行?