首页 > 解决方案 > 为什么 Intent 在 Android 中不显示?

问题描述

如果我从(启动 PIN.class Activity)启动 Intent,则不会显示布局。为什么 Toast 会立即显示在每个 Android 上,但我的 PIN.class 布局却没有?

我认为这与我的 PIN.class 有关。更改 setPriority 也不能解决问题。或者可能是,我们在 Runnable 函数中启动必要的代码?

谢谢!

// PIN.java
public class PIN extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // Star Layout HERE 
        setContentView(R.layout.activity_settings);

    }
    
}

// MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {

    Handler handler = new Handler(Looper.getMainLooper());
        Runnable runnableCode = new Runnable() {
            
        @Override
        public void run() {
                
            Toast.makeText(context, "got toasted", Toast.LENGTH_LONG).show();

                Intent intent = new Intent(context, PIN.class);

                IntentFilter filter = new IntentFilter(String.valueOf(intent));
                filter.setPriority(1000);// what you want to set
                
                    /**
                    * ...starting the important Intent
                    **/
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        startForegroundService(intent);
                    } 
                    
                    else {
                        startService(intent);
                    }
                    
                    startActivity(intent);
        }
    };
}

// activity_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textViewPinSettingsHeadline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/setPin_label"
        tools:layout_editor_absoluteX="62dp"
        tools:layout_editor_absoluteY="60dp"
        android:textSize="30dp"
        android:textColor="@color/colorPrimary"
        />

    <TextView
        android:id="@+id/textViewPinSettings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Old PIN: "
        tools:layout_editor_absoluteX="62dp"
        tools:layout_editor_absoluteY="60dp" />

    <EditText
        android:id="@+id/oldpininput"
        android:hint="1234"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        tools:layout_editor_absoluteX="154dp"
        tools:layout_editor_absoluteY="60dp" />

    <TextView
        android:id="@+id/textViewPinSettings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New PIN: "
        tools:layout_editor_absoluteX="62dp"
        tools:layout_editor_absoluteY="129dp" />

    <EditText
        android:id="@+id/newpininput"
        android:hint="0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        tools:layout_editor_absoluteX="154dp"
        tools:layout_editor_absoluteY="129dp" />

    <Button
        android:id="@+id/buttonSetPin"
        android:layout_width="match_parent"
        android:layout_height="55dp"

        android:text="save PIN"
        app:layout_constraintBottom_toBottomOf="parent" />

</LinearLayout>

// AndroidManifest.xml
// ...
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
// ...

标签: javaandroidandroid-intentprocesslooper

解决方案


推荐阅读