Я использую Android Studio и пытаюсь отобразить температуру батареи в строке состояния в качестве уведомления. Я успешно выполнил эту задачу, но есть одна проблема. Температура отображается как 0,0 по Цельсию ... Вот мой код:Android Studio - попытка отображать температуру батареи в строке состояния как уведомление
public class SettingsActivity extends ActionBarActivity implements
OnClickListener
{
Button start, clear;
Notification noti;
NotificationManager nmgr;
public static final int NOTIFICATION_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.onActivityCreateSetTheme(this);
setContentView(R.layout.activity_settings);
getInit();
nmgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti = new Notification(R.drawable.flame, "Battery Temperature", System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
float temp = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0))/10;
noti.setLatestEventInfo(this, "Battery Temperature", String.valueOf(temp) + " C", pIntent);
noti.flags = Notification.FLAG_ONGOING_EVENT;
}
public void getInit()
{
findViewById(R.id.btn).setOnClickListener(this);
findViewById(R.id.btn2).setOnClickListener(this);
start = (Button) findViewById(R.id.btn3);
clear = (Button) findViewById(R.id.btn4);
start.setOnClickListener(this);
clear.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btn:
new AlertDialog.Builder(this)
.setTitle("Info")
.setMessage("Battery Info © 2015 Natan Rosenfeld")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
break;
case R.id.btn2:
Intent startcredits = new Intent(this, CreditsActivity.class);
startActivity(startcredits);
break;
case R.id.btn3:
nmgr.notify(NOTIFICATION_ID,noti);
break;
case R.id.btn4:
nmgr.cancel(NOTIFICATION_ID);
}
}
}
Вы уверены, что устройство имеет датчик температуры? –
Да, я могу отображать температуру в textView, но не в строке состояния. –