0

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);
           }
    }
1
  • 2
    Si quieres desarrollar aplicaciones Android es importante usar el LogCat para determinar problemas en tu aplicación, de lo contrario podrías tardar tiempo innecesario en resolver un problema, agrega tu mensaje de error desplegado en el LogCat Daniel!
    – Jorgesys
    el 14 oct. 2016 a las 3:10

1 respuesta 1

1

Es importante agregues tu LogCat, esto para que los desarrolladores puedan definir cual es el problema. A primera vista el listener para ejecutar clic en el boton esta incorrectamente definido:

Button s = (Button) findViewById(R.id.mainButton1);
        s.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View p1)
                {
                    createNotification();
                }
            });

Debe ser:

Button s = (Button) findViewById(R.id.mainButton1);
    s.setOnClickListener(new View.OnClickListener() { //Aquí! ***
        @Override
        public void onClick(View v) {
            createNotification();
        }
    });

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.