gpt4 book ai didi

android - 通过挂起的 Intent 更改 Activity 的布局

转载 作者:行者123 更新时间:2023-11-30 04:57:41 25 4
gpt4 key购买 nike

我正在尝试通过使用未决 Intent 更改发送通知的同一 Activity 的颜色。我对其工作原理的看法如下:

1) AuthenticationMode 类发送一个带有 pendingIntent 的通知,其中包含一个额外的整数

2) 通知被发送,用户点击它然后返回到相同的 Activity

3) onNewIntent 被调用,获取额外的整数,并根据从 intent 中的额外值获得的值,将布局颜色更改为“#00FF00”(如果为 1)或“#f35336”(如果为 0)。

据我了解,这是正确执行的,但由于某种原因,背景颜色仍然没有改变。

当我使用方法 authenticate (onclick: authenticate) 调用 colorChanger(int result) 时它起作用,但是当使用通知/未决 Intent 时,它进入颜色转换器和 logcat 打印:Log。 d("colorchanger","re​​sult 1 entered if loop and past color changer");, 但即便如此它也不起作用

我在这里提供有关该问题的更多信息:

public class AuthenticationMode extends Activity {

//For device enabled lockscreen
private DevicePolicyManager mgr = null;
private ComponentName cn= null;

//For Notification
private final String CHANNEL_ID = "authenticate";
private final int NOTIFICATION_ID = 002;
private boolean isRecording = false;

private ConstraintLayout mConstraintLayout;
private Button mAuthenticationButton;
private PowerManager.WakeLock mWakeLock;

//Temporary variable
private int authenticationcounter = 0;

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

// battery power setting
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sensors_data_logger:wakelocktag");
mWakeLock.acquire();

cn = new ComponentName(this, AdminReceiver.class);
mgr = (DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
}

@Override
protected void onDestroy() {

if (mWakeLock.isHeld()) {
mWakeLock.release();
}
super.onDestroy();
}

private void initializeViews() {

mAuthenticationButton = (Button) findViewById(R.id.button_authenticate);
mConstraintLayout = (ConstraintLayout) findViewById(R.id.authentication_layout);

}

public void authenticate(View view){
if(mgr.isAdminActive(cn)) {
mgr.lockNow();
displayNotification(1);
} else {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getString(R.string.permission_explanation));
startActivity(intent);
}

}

private void displayNotification(int result){
createNotificationChannel();
Intent authenticationIntent = new Intent(this, AuthenticationMode.class);
Log.d("Passedresult", "result = " + result);
authenticationIntent.putExtra("authentication result", result);
authenticationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent authenticationPendingIntent = PendingIntent.getActivity(this, UUID.randomUUID().hashCode(), authenticationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("Hi, phone owner");
builder.setContentText("Click me to authenticate");
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setAutoCancel(true);
builder.setContentIntent(authenticationPendingIntent);


NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID,builder.build());
}

private void createNotificationChannel(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
CharSequence name = "Stop Notification";
String description = "Stops the data recording";
int importance = NotificationManager.IMPORTANCE_HIGH;

NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,name,importance);
notificationChannel.setDescription(description);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.createNotificationChannel(notificationChannel);
}
}

protected void onNewIntent(Intent intent){
super.onNewIntent(intent);
Log.d("ONNEWINTENT", "onNewIntent is called");
setIntent(intent);
processIntent();

}

private void processIntent(){
Intent intent = getIntent();
Log.d("processIntent", "processIntent called");
if(intent!=null){
Log.d("nonnull", "non null intent");
int result = intent.getIntExtra("authentication result", 2);
Log.d("gotextra","Got result: " + result);
if (result == 1){
Log.d("ifloop","result 1 entered if loop");
colorChanger(result);
authenticationcounter += 1;
} else if (result == 0){
Log.d("ifloop","result 0 entered if loop");
colorChanger(result);
authenticationcounter +=1;
}
}
}

private void colorChanger(int result){
if (result == 1){
mConstraintLayout.setBackgroundColor(Color.parseColor("#00FF00"));
Log.d("colorchanger","result 1 entered if loop and past color changer");
authenticationcounter += 1;
} else if (result == 0){
mConstraintLayout.setBackgroundColor(Color.parseColor("#f35336"));
authenticationcounter +=1;
}
}


}


布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AuthenticationMode"
android:id="@+id/authentication_layout">

<TextView
android:id="@+id/authenticationTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:fontFamily="@font/roboto"
android:text="@string/authenticateLabel"
android:textAlignment="center"
android:textColor="#000000"
android:textColorHint="#00000000"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button_authenticate"
android:layout_width="300dp"
android:layout_height="45dp"
android:layout_marginStart="16dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="10dp"
android:background="#FFFFFF"
android:fontFamily="@font/roboto_black"
android:onClick="authenticate"
android:text="@string/start_title"
android:textAlignment="center"
android:textColor="#3D5AFE"
android:textSize="24sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>



</androidx.constraintlayout.widget.ConstraintLayout>

我知道:displayNotification(1); 我正在传递一个静态值,但我这样做是为了测试。即使那样,颜色也不会更改为:“#00FF00”

最佳答案

试试这个:

private void colorChanger(int result){
if (result == 1){
mConstraintLayout.setBackgroundColor(getResources().getColor(R.color.colorOne));
Log.d("colorchanger","result 1 entered if loop and past color changer");
authenticationcounter += 1;
} else if (result == 0){
mConstraintLayout.setBackgroundColor(getResources().getColor(R.color.colorTwo));
authenticationcounter +=1;
}
}

将这两种颜色添加到您的 color.xml 文件中。如下图

    <color name="colorOne">#00FF00</color>
<color name="colorTwo">#f35336</color>

关于android - 通过挂起的 Intent 更改 Activity 的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58848894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com