首页 > 解决方案 > 如何使用通知侦听器服务在动态生成的文本视图中使用

问题描述

自从我开始开发应用程序以保存所有 Instagram 通知以来已经一周了,这样如果发件人取消发送消息,我就可以访问已删除的消息。

我发现我应该使用一种叫做通知侦听器服务的东西。我已经阅读了很多关于它的文章,并且我还阅读了有关通知的整个 Android 开发人员文档,但是它太复杂了,我仍然不知道如何在我的应用程序中使用这个东西。

我想在列表中显示所有通知并动态生成文本视图并用通知填充它们。

有人可以帮帮我吗?

这是我的主要活动:

public class MainActivity extends Activity {

private TextView txtView;
private NotificationReceiver nReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtView = (TextView) findViewById(R.id.textView);
    nReceiver = new NotificationReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
    registerReceiver(nReceiver,filter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(nReceiver);
}



public void buttonClicked(View v){

    if(v.getId() == R.id.btnCreateNotify){
        NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder ncomp = new NotificationCompat.Builder(this);
        ncomp.setContentTitle("My Notification");
        ncomp.setContentText("Notification Listener Service Example");
        ncomp.setTicker("Notification Listener Service Example");
        ncomp.setSmallIcon(R.drawable.ic_launcher);
        ncomp.setAutoCancel(true);
        nManager.notify((int)System.currentTimeMillis(),ncomp.build());
    }
    else if(v.getId() == R.id.btnClearNotify){
        Intent i = new Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
        i.putExtra("command","clearall");
        sendBroadcast(i);
    }
    else if(v.getId() == R.id.btnListNotify){
        Intent i = new Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
        i.putExtra("command","list");
        sendBroadcast(i);
    }


}

class NotificationReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        String temp = intent.getStringExtra("notification_event") + "\n" + txtView.getText();
        txtView.setText(temp);
    }
}

这也是我的 NotificationsListener 类:

公共类 NLService 扩展 NotificationListenerService {

private String TAG = this.getClass().getSimpleName();
private NLServiceReceiver nlservicereciver;
@Override
public void onCreate() {
    super.onCreate();
    nlservicereciver = new NLServiceReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.kpbird.nlsexample.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
    registerReceiver(nlservicereciver,filter);
}

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(nlservicereciver);
}

@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.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
    i.putExtra("notification_event","onNotificationPosted :" + sbn.getPackageName() + "\n");
    sendBroadcast(i);

}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    Log.i(TAG,"********** onNOtificationRemoved");
    Log.i(TAG,"ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText +"\t" + sbn.getPackageName());
    Intent i = new  Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
    i.putExtra("notification_event","onNotificationRemoved :" + sbn.getPackageName() + "\n");

    sendBroadcast(i);
}

class NLServiceReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getStringExtra("command").equals("clearall")){
                NLService.this.cancelAllNotifications();
        }
        else if(intent.getStringExtra("command").equals("list")){
            Intent i1 = new  Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
            i1.putExtra("notification_event","=====================");
            sendBroadcast(i1);
            int i=1;
            for (StatusBarNotification sbn : NLService.this.getActiveNotifications()) {
                Intent i2 = new  Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
                i2.putExtra("notification_event",i +" " + sbn.getPackageName() + "\n");
                sendBroadcast(i2);
                i++;
            }
            Intent i3 = new  Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
            i3.putExtra("notification_event","===== Notification List ====");
            sendBroadcast(i3);

        }

    }
}

}

标签: javaandroidnotificationsnotification-listener

解决方案


推荐阅读