我环顾四周,无法找到使用广播接收器覆盖现有远程视图TextView的方法。
我正在尝试在我的MainActivity中获取远程视图,并在我按下通知上的按钮时使用新值更新它以显示在通知中。我尝试在“Button_listener_plus”中重新创建通知管理器和远程视图,希望它会覆盖现有的,但当然它不会使应用程序崩溃...我还试图让远程视图公开看如果我可以在“Button_listener_plus”中重复使用它,但又可以让它工作。我描述的前一种方法显示在下面的代码中。
以下是我遇到问题的代码片段:
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());
}
});
}
}
广播接收者:
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
}
}
我看了下面的内容,试图解决问题:
Android BroadcastReceiver onReceive Update TextView in MainActivity