我在清单文件中添加了一些像AACTION_POWER_CONNECTED的迭代,我希望在连接充电器时获得通知。这是我的代码。
public class AwesomeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Inside onReceive", Toast.LENGTH_LONG).show();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("This is A Notification");
mBuilder.build();
}
}
每次连接充电器但未收到通知时,我都会收到Toast
。由于我是Android的新手,我不知道如何实现这一目标。
答案 0 :(得分:0)
试试这段代码:
public class AwesomeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Inside onReceive", Toast.LENGTH_LONG).show();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("This is A Notification");
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
}
}
其实你创建了Notification对象但从未解雇它:)
因此,您只需要与notify()
保持联系。