【问题标题】:Changing the layout of an activity through pending intent通过待定意图更改活动的布局
【发布时间】:2019-11-14 03:23:20
【问题描述】:

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

1) AuthenticationMode 类发送一个包含一个额外整数的 pendingIntent 通知

2) 通知被发送,用户点击它然后返回到同一个活动

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

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

当我使用方法 authenticate (onclick: authenticate) 来调用 colorChanger(int result) 时,它可以工作,但是当使用通知/待定意图时,它会进入颜色转换器和 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”

【问题讨论】:

    标签: android android-layout notifications background android-pendingintent


    【解决方案1】:

    试试这个:

    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>
    

    【讨论】:

    • 感谢您的回答,但不幸的是它不起作用。此外,不推荐使用 getResources().getColor(color)。
    • 很抱歉,我看到这并没有被弃用。哪个版本显示它已被弃用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多