如何在通知栏中显示电池状态?

时间:2017-12-22 17:31:00

标签: android android-layout android-broadcast android-broadcastreceiver

我想在使用Android Studio 3.0.1的应用的通知栏中显示电池状态。

电池状态代码为:

BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        int lol = intent.getIntExtra("level",0);
    }
};

对于通知栏,我所拥有的是:

public class batterystatus extends Activity {

    Button notify_button;
    NotificationManager nm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_batterystatus);
        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        notify_button = (Button)findViewById(R.id.notifybutton);
        registerReceiver(receiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        notify_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Notification.Builder builder = new Notification.Builder(batterystatus.this)
                        .setSmallIcon(android.R.drawable.stat_sys_warning)
                        .setContentText("This is in Detail")
                        .setContentTitle("This is the title");
                Intent intent = new Intent(batterystatus.this,batterystatus.class);
                PendingIntent pi = PendingIntent.getActivity(batterystatus.this,0,intent,0);
                builder.setContentIntent(pi);
                Notification notification = builder.build();
                notification.defaults = Notification.DEFAULT_VIBRATE;
                notification.defaults = Notification.DEFAULT_SOUND;
                nm.notify(0,notification);
            }
        });
    }
}

我该怎么做或接近它?

0 个答案:

没有答案