【问题标题】:how to show to battery status on text view?如何在文本视图中显示电池状态?
【发布时间】:2015-01-15 22:56:26
【问题描述】:

实际上,我正在尝试制作一个 android 应用程序在电池电量变为 50% 时显示文本,我使用文本视图显示味精,但在加载应用程序时不幸停止了。

这是我的代码:

Java 文件。

package com.example.abhi.batteryalarm;

import android.content.Intent;
import android.os.BatteryManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class Battery_Alarm extends ActionBarActivity {
    Intent batteryStatus;
    TextView txtView;
    String hello;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_battery__alarm);
        txtView=(TextView)findViewById(R.id.txtView);
        hello="This is my first project";

        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        if (level==50)
        {
            txtView.setText(hello);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_battery__alarm, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

活动文件:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Battery_Alarm">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        android:textSize="62sp"
        android:id="@+id/txtView"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="125dp" />

</RelativeLayout>

我需要做什么?

【问题讨论】:

  • 使用广播接收器获取当前状态。
  • 什么异常。粘贴日志
  • 你从哪里得到这个代码?当你有错误时总是发布 logcat,我 thuink batteryStatus 没有分配所以它是空的,这就是他的问题,
  • 它在运行时没有显示错误,但是当我在手机上运行该应用程序时显示“不幸的是,电池警报已停止”,如何使用广播接收器?请帮助

标签: android performance


【解决方案1】:

添加这个,

batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

之前,

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

它会起作用的。

【讨论】:

  • 现在代替显示文本我想播放声音我将如何继续?我得到了这个代码:MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);媒体播放器.start();但无法理解!请帮忙
  • 所以你是说音乐没有播放???因为代码对我来说看起来不错。
【解决方案2】:

这是导致错误的行:

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

这是因为 batteryStatus 意图从未初始化,因此为空。

查看有关获取电池电量的答案:https://stackoverflow.com/a/4047790/1239966

【讨论】:

    【解决方案3】:

    您需要注册电池状态。

    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);
    

    【讨论】:

    • 谢谢!!!!它现在可以工作了!,,,现在假设我想在后台运行这个应用程序,这样当电池电量变为 50% 时,应用程序将打开并显示文本!!!可以吗???/
    猜你喜欢
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多