2

I have looked all around and can not find a way to overwrite an existing Remote View TextView using a Broadcast Receiver.

I'm trying to get the Remote View inside my MainActivity and update it with a new value to be displayed inside the notification when I press the button on the notification. I tried to recreate the notification manager and the remote view inside "Button_listener_plus", hoping it would override the existing one, but of course it doesn't and make the app crash... I also tried to make the remote view public to see if I could reuse it in "Button_listener_plus" but again could get it to work. The former method I described is shown in the code below.

Here is a snippet of the code I'm having trouble with:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    //Notification stuff
    private NotificationCompat.Builder builder;
    public NotificationManager notificationManager;
    private int notification_id;
    private int notification_id2;
    private int notification_id3;
    private RemoteViews remoteViews;
    private Context context;


    //debug tags
    private String TAG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //If permission is needed, ask
        /*
        boolean canDo =  Settings.System.canWrite(this);
        if (false == canDo)
        {
            Intent grantIntent = new   Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
            startActivity(grantIntent);
        }
        */

        context = this;
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        remoteViews = new RemoteViews(getPackageName(),R.layout.custom_notif);
        //remoteViews.setImageViewResource(R.id.notif_icon,R.mipmap.ic_launcher);
        remoteViews.setTextViewText(R.id.text_display_bright,"LVL");

        findViewById(R.id.button_start_n).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //plus
                notification_id = (int) System.currentTimeMillis();
                Intent button_intent = new Intent("button_clicked_plus");
                button_intent.putExtra("id",notification_id);
                PendingIntent button_intent_plus = PendingIntent.getBroadcast(context,notification_id,
                        button_intent,0);
                remoteViews.setOnClickPendingIntent(R.id.button_max_10,button_intent_plus);

                //minus
                notification_id2 = (int) System.currentTimeMillis() + 2;
                Intent button_intent_minus = new Intent("button_clicked_minus");
                button_intent_minus.putExtra("id2",notification_id2);
                PendingIntent button_pending_event_minus = PendingIntent.getBroadcast(context,notification_id2,
                        button_intent_minus,0);
                remoteViews.setOnClickPendingIntent(R.id.button_min_10,button_pending_event_minus);

                //Notif Check
                notification_id3 = (int) System.currentTimeMillis() + 3;
                Intent button_intent_check = new Intent("button_clicked_check");
                button_intent_check.putExtra("id3",notification_id3);
                PendingIntent button_pending_event_check = PendingIntent.getBroadcast(context,notification_id3,
                        button_intent_check,0);
                remoteViews.setOnClickPendingIntent(R.id.button_notif_check,button_pending_event_check);


                Intent notification_intent = new Intent(context,MainActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(context,0,notification_intent,0);

                //build notification & insert remote view (custom layout)
                builder = new NotificationCompat.Builder(context);
                builder.setSmallIcon(R.drawable.ic_stat_name)
                        //.setAutoCancel(true)
                        //.setCustomBigContentView(remoteViews)
                        .setCustomContentView(remoteViews)
                        .setContentIntent(pendingIntent)
                        .setPriority(Notification.PRIORITY_MIN)
                ;
                notificationManager.notify(notification_id,builder.build());

            }
        });

    }
}

And the Broadcast Receiver:

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.NotificationCompat;
import android.widget.RemoteViews;
import android.widget.Toast;
import static android.content.Context.NOTIFICATION_SERVICE;

public class Button_listener_plus extends BroadcastReceiver {

    private int plus_id;

    //
    private NotificationCompat.Builder builder;
    private NotificationManager notificationManager;
    private RemoteViews remoteViews;
    private Context context;
    //


    @Override
    public void onReceive(Context context, Intent intent) {
        //Toast.makeText(context, "button_clicked!", Toast.LENGTH_SHORT).show();

        int curBrightnessValue = 0;
        try {
            curBrightnessValue = Settings.System.getInt(context.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }

        //int rounded = ((curBrightnessValue + 9) / 10 ) * 10;
        //int result = Math.min(255, rounded + 10);

        int result = Math.min(250, curBrightnessValue + 25);

        android.provider.Settings.System.putInt(context.getContentResolver(),
                android.provider.Settings.System.SCREEN_BRIGHTNESS, result);
        //Toast.makeText(MainActivity.this,"" + result,Toast.LENGTH_SHORT).show();

        int percent_scale = (result/25)*10;

        Toast toasty = Toast.makeText(context, "" + percent_scale, Toast.LENGTH_SHORT);
        toasty.show();


        //...not working... crashes
        notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.custom_notif);
        remoteViews.setTextViewText(R.id.text_display_bright,""+result);
        notificationManager.notify(intent.getExtras().getInt("id"),builder.build());
        //delete


    }

}

I looked at the following to try to figure things out:

Android BroadcastReceiver onReceive Update TextView in MainActivity

Update TextView in Broadcast Receivers

Community
  • 1
  • 1
UberNoob
  • 25
  • 3

0 Answers0