Hola estoy teniendo problemas intentando de mostrar la temperatura de mi batería en una notificación cuando intento abrir la app me fuerza el cierre aqui está mi código espero puedan ayudarme
public class MainActivity extends Activity
{
NotificationCompat.Builder nBuilder;
Notification noti;
NotificationManager nm;
TextView t;
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
float temp = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)) / 10;
MainActivity.this.t.setText("Battery Temperature: " + temp + "°C");
MainActivity.this.nBuilder.setContentTitle(String.valueOf(temp) + "°C");
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(this.batteryInfoReceiver, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
this.t = (TextView) findViewById(R.id.mainTextView1);
Button s = (Button) findViewById(R.id.mainButton1);
s.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View p1)
{
createNotification();
}
});
}
public void createNotification()
{
this.nBuilder = new NotificationCompat.Builder(this);
nBuilder.setSmallIcon(R.drawable.ic_launcher);
nBuilder.setContentText("Click to open app");
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
nBuilder.setContentIntent(notifyPendingIntent);
this.noti = nBuilder.build();
this.nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags = Notification.FLAG_ONGOING_EVENT;
nm.notify(1,noti);
}
}