我一直在尝试通过创建一个应用程序来学习如何在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.testing.notify.NOTIFICATION");
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.setSmallIcon(R.mipmap.ic_launcher);
ncomp.setContentTitle("Notify");
ncomp.setContentText("Notification Listener Service Example");
ncomp.setTicker("Notification Listener Service Example");
ncomp.setAutoCancel(true);
nManager.notify((int)System.currentTimeMillis(),ncomp.build());
}
else if(v.getId() == R.id.btnClearNotify){
Intent i = new Intent("com.testing.notify.NOTIFICATION");
i.putExtra("command","clearall");
sendBroadcast(i);
}
else if(v.getId() == R.id.btnListNotify){
Intent i = new Intent("com.testing.notify.NOTIFICATION");
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);
}
}
这是我的通知服务类的代码
public class NotificationService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
private NLServiceReceiver nlservicereceiver;
@Override
public void onCreate() {
super.onCreate();
nlservicereceiver = new NLServiceReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.testing.notify.NOTIFICATION");
registerReceiver(nlservicereceiver,filter);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(nlservicereceiver);
}
@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.testing.notify.NOTIFICATION");
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.testing.notify.NOTIFICATION");
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")){
NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
}
else if(intent.getStringExtra("command").equals("list")){
Intent i1 = new Intent("com.testing.notify.NOTIFICATION");
i1.putExtra("notification_event","=====================");
sendBroadcast(i1);
int i=1;
for (StatusBarNotification sbn : NotificationService.this.getActiveNotifications()) {
Intent i2 = new Intent("com.testing.notify.NOTIFICATION");
i2.putExtra("notification_event",i +" " + sbn.getPackageName() + "n");
sendBroadcast(i2);
i++;
}
Intent i3 = new Intent("com.testing.notify.NOTIFICATION");
i3.putExtra("notification_event","===== Notification List ====");
sendBroadcast(i3);
}
}
}
我一直试图解决这个问题,所以任何帮助都会受到赞赏。