【问题标题】:How create a permanent notification to show battery level如何创建永久通知以显示电池电量
【发布时间】:2013-07-03 15:21:55
【问题描述】:

我被困在通知的创建中。只想在状态栏中显示电池电量(通知栏)通知已永久存在。这是代码的一部分:

private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

            int  level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

              String[] status = {
                "Level: "+level
                 . . .
               };

             NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

             Notification notification = new Notification(R.drawable.ic_launcher,"Level",System.currentTimeMillis());

             notification.flags = Notification.FLAG_ONGOING_EVENT;

             Intent i = new Intent(context, MainActivity.class); 

             PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0);

             notification.setLatestEventInfo(getApplicationContext(), +level+"%", penInt);

             notifi.notify(215,notification);


            }
};

正确吗?例如在清单中是否还有其他要添加的内容?我没有指定的通知类,所以我以这种方式写了这一行:Intent i = new Intent(context, MainActivity.class); 但我不知道它是否正确。

【问题讨论】:

    标签: android statusbar android-notifications batterylevel


    【解决方案1】:

    是的,您需要添加:

    <uses-permission android:name="android.permission.BATTERY_STATS" />
    

    更多信息在这里:

    Get battery level only once using Android SDK

    【讨论】:

    • 好的,谢谢。一件事,现在Notification notification = new Notification(R.drawable.ic_launcher,+level+"%",System.currentTimeMillis()) 我只想显示关卡而不是图标。实际上它只显示状态栏中的图标电池。有办法删除吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多