Android BroadCastReceiver ไม่ทำงานกับการกระทำแบบกำหนดเอง

ฉันกำลังพยายามใช้ BroadCastReceiver แบบง่าย ๆ ด้วยการกระทำที่กำหนดเองซึ่งจะแสดงขนมปังปิ้งในวิธี onReceive() ของตัวรับ แต่มันไม่ทำงานด้วยเหตุผลลึกลับบางอย่าง !!

กิจกรรมหลักมีเพียงปุ่มที่ถ่ายทอดเจตนาเมื่อคลิก ฉันไม่แน่ใจว่าเป็นปัญหาในการตั้งชื่อการดำเนินการหรือไม่ ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชม

กิจกรรมหลัก :

   package com.example.tsupt.bcr;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    public void broadcastIntent(View view){
        Intent intent = new Intent();
        intent.setAction("com.example.tsupt.bcr.CUSTOM_INTENT");
        sendBroadcast(intent);
    }

}

เครื่องรับกระจายเสียง :

public class BR extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"It worked",Toast.LENGTH_LONG).show();
        System.out.println("It worked");

}}

รายการ:

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tsupt.bcr">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <receiver android:name="com.example.tsupt.bcr.BR">
                <intent-filter>
                    <action android:name="com.example.tsupt.bcr.CUSTOM_INTENT">
                    </action>
                </intent-filter>

            </receiver>
        </activity>

    </application>

</manifest>

ปุ่มจากไฟล์เลย์เอาต์:

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="broadcastIntent"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="180dp"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent" />

person Tareq Sulaiman    schedule 19.09.2017    source แหล่งที่มา


คำตอบ (2)


จริงๆ แล้วมันก็ได้ผล ดูเหมือนว่าปัญหาจะเกิดขึ้นกับ huawei Honor 5x ของฉันที่ฉันใช้ทดสอบแอปของฉัน มีวิธีการประหยัดแบตเตอรี่ที่โทรศัพท์ Huawei ใช้และดูเหมือนว่าจะเป็นต้นตอของปัญหา

person Tareq Sulaiman    schedule 20.09.2017
comment
คุณสามารถแก้ไขปัญหาได้โดยปิดการเพิ่มประสิทธิภาพแบตเตอรี่ใช่ไหม ตอนนั้นคุณใช้ Android เวอร์ชันใดอยู่ ฉันมีปัญหาคล้ายกันกับโทรศัพท์ Huawei หลายรุ่น และดูเหมือนว่าเราไม่สามารถทำให้มันใช้งานได้ - person Markus Penguin; 08.03.2018
comment
จริงๆ แล้วไม่ ฉันทำไม่ได้ Huawei ไม่ได้ให้คุณควบคุมคุณสมบัติการเพิ่มประสิทธิภาพแบตเตอรี่ทั้งหมด ฉันเพิ่งเริ่มใช้โทรศัพท์เครื่องอื่นในการทดสอบ ฉันยังประสบปัญหามากมายกับโทรศัพท์ Huawei ที่ใช้บริการพื้นหลังด้วย ดังนั้นโดยพื้นฐานแล้วฉันจึงเลิกใช้โทรศัพท์ Huawei เพื่อทดสอบแอป Android - person Tareq Sulaiman; 08.03.2018

ในรายการของคุณ องค์ประกอบ <receiver> ควรเป็นองค์ประกอบระดับเดียวกันขององค์ประกอบ <activity> แทนที่จะเป็นองค์ประกอบย่อย:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tsupt.bcr">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name="com.example.tsupt.bcr.BR">
        <intent-filter>
            <action android:name="com.example.tsupt.bcr.CUSTOM_INTENT">
            </action>
        </intent-filter>

    </receiver>

</application>

</manifest>
person sdabet    schedule 19.09.2017
comment
ขอบคุณมากสำหรับการตอบกลับ แต่น่าเสียดายที่มันไม่ได้ผลเช่นกัน - person Tareq Sulaiman; 19.09.2017
comment
เมื่อเรียกใช้แอป ฉันได้รับสิ่งนี้ในจอภาพ Android: com.example.tsupt.bcr I/SendBroadcastPermission: action:com.example.tsupt.bcr.CUSTOM_INTENT, mPermissionType:0 นั่นหมายความว่ามีอะไรผิดปกติเกิดขึ้นหรือไม่ - person Tareq Sulaiman; 19.09.2017
comment
ใช่มันได้ผล ปัญหาเกิดขึ้นกับอุปกรณ์ที่ฉันกำลังทดสอบแอป - person Tareq Sulaiman; 20.09.2017
comment
น่าประหลาดใจที่ AFAIK องค์ประกอบตัวรับจะถูกละเว้นหากฝังอยู่ในองค์ประกอบกิจกรรม - person sdabet; 21.09.2017