Android 如何在文本视图中显示电池状态?
事实上,我正试图让android应用程序在电池电量达到50%时显示文本,我使用文本视图来显示消息,但在加载应用程序时,不幸的是,它停止了 这是我的密码: Java文件Android 如何在文本视图中显示电池状态?,android,performance,Android,Performance,事实上,我正试图让android应用程序在电池电量达到50%时显示文本,我使用文本视图来显示消息,但在加载应用程序时,不幸的是,它停止了 这是我的密码: Java文件 package com.example.abhi.batteryalarm; import android.content.Intent; import android.os.BatteryManager; import android.support.v7.app.ActionBarActivity; import andro
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>
我需要做什么?这是导致错误的一行:
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
这是因为batteryStatus意图从未初始化,因此为空
查看有关获取电池电量的回答:添加此选项
batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
以前,
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
它会工作。您需要注册batteryStatus
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
使用广播接收器,您可以获得当前状态。有什么异常。把日志粘贴到哪里你得到这个代码了吗?post logcat总是当你出错时,我认为没有分配电池状态,所以它为空,这就是问题所在,它在运行时没有显示错误,但当我在手机上运行该应用时,它会显示“不幸的是,电池警报已停止”,如何使用广播接收器?plase helpnow代替显示文本我想播放声音我将如何继续?我得到了以下代码:MediaPlayer=MediaPlayer.create(context,R.raw.sound\u file\u 1);mediaPlayer.start();但是我不明白!请帮忙,你是说音乐没有播放???因为代码在我看来很好。谢谢!!!!现在开始工作了!,,,现在假设我想在后台运行这个应用程序,这样当电池电量达到50%时,应用程序就会打开并显示文本!!!有可能吗/