当前位置: 首页 > 面试题库 >

为什么Activity类内的BroadCastReceiver不能接收LocalBroadCastManager从IntentService发送的意图?

昝晗昱
2023-03-14
问题内容

ChooseLanguageFragment.java

Intent explicitGetNumberServiceIntentUSA = new Intent(getActivity(), GetNumberService.class);
explicitGetNumberServiceIntentUSA.putExtra("country", "USA");
getActivity().startService(explicitGetNumberServiceIntentUSA);

这就是调用GetNumberService的方式。

这是从GetNumberService.java传递arraylist的方式

Intent mobileNumbersIntent = new Intent();
mobileNumbersIntent.putStringArrayListExtra("mobileNumbers", mobileNumberList);
mobileNumbersIntent.setAction(ChooseNumber1.MobileNumberBroadcastReceiver.MOBILE_NO_RECEIVER);
mobileNumbersIntent.addCategory(Intent.CATEGORY_DEFAULT);
mobileNumbersIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is needed when callling startActivity() outisde an Activity            
sendBroadcast(mobileNumbersIntent);

在onHandleIntent()方法中。

GetNumberService可以完美地完成其工作。

ChooseNumber1.java

public class ChooseNumber1 extends AppCompatActivity {
    private MobileNumberBroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter intentFilter = new IntentFilter(MobileNumberBroadcastReceiver.MOBILE_NO_RECEIVER);
        intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
        receiver = new MobileNumberBroadcastReceiver();
        registerReceiver(receiver, intentFilter);
        setContentView(R.layout.activity_choose_number1);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onDestroy(){
        unregisterReceiver(receiver);
        super.onDestroy();
    }

    public class MobileNumberBroadcastReceiver extends BroadcastReceiver {

        public static final String MOBILE_NO_RECEIVER = "abcd";
        ArrayList<String> mobileNumbersList = new ArrayList<>();
        ListView mobileNumberListView;
        ArrayAdapter<String> mobileNumberAdapter;

        @Override
        public void onReceive(Context context, Intent intent) {
          //  if(intent.getAction().equals(GetNumberService.MOBILE_NUMBER_LIST_PASS_ACTION)){
                mobileNumbersList = intent.getStringArrayListExtra("mobileNumberList");
         //   }
            mobileNumberAdapter = new ArrayAdapter<String>(new ChooseNumbers(),
                    R.layout.list_item_numbers, R.id.list_item_number_textview, mobileNumbersList);

            mobileNumberListView = (ListView) findViewById(R.id.listViewNumberList);
            mobileNumberListView.setAdapter(mobileNumberAdapter);
        }
    }

}

activity_choose_number1.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.a2ndnumber.a2ndnumber.ChooseNumbers">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_choose_number1" />

</android.support.design.widget.CoordinatorLayout>

content_choose_number1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.a2ndnumber.a2ndnumber.ChooseNumber1"
    tools:showIn="@layout/activity_choose_number1">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listViewNumberList"/>

</RelativeLayout>

PS 2:

问题是,我无法在ChooseNumber1.java或ChooseNumbersFragment.java中收到mobileNumbersIntent。我认为问题出在BroadcastManager.java。如果我进行调试,它将转到Handler.java和其他几个类,最后是mId
= -1,并且堆栈跟踪中没有错误。我被感动了

PS:AndroidManifest.xml

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

    <!-- TODO : Add permissions -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- Contacts -->
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".ChooseLanguage"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
        <activity
            android:name=".Choose_Country"
            android:label="@string/app_name"
            android:parentActivityName=".ChooseLanguage"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.a2ndnumber.a2ndnumber.ChooseLanguage" />
        </activity>
        <activity
            android:name=".MainActivity"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".service.GetNumberService" />

        <activity
            android:name=".ChooseNumber1"
            android:label="@string/title_activity_choose_number1"
            android:parentActivityName=".Choose_Country"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.a2ndnumber.a2ndnumber.Choose_Country" />
        </activity>
    </application>

</manifest>

PS:在查看了Stackoverflow中的大量示例和答案之后,我对代码进行了很多修改。这是我的最新代码。看起来问题出在意图过滤器和/或使用动作获取意图。请帮我。谢谢。


问题答案:

所需的流程不需要广播和接收器设置。由于将ChooseNumber1
ActivityService完成工作之后启动,因此您只需要将附加ArrayListIntent用于启动上即可ChooseNumber1。然后可以在中检索该列表onCreate(),并且可以立即初始化your
ListView及其列表Adapter,而不必尝试等待广播。

例如,在Service清单中,您的中的清单被组合后:

Intent mobileNumbersIntent = new Intent(this, ChooseNumber1.class);
mobileNumbersIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mobileNumbersIntent.putStringArrayListExtra("mobileNumbers", mobileNumberList);
startActivity(mobileNumbersIntent);

然后,在Activity

public class ChooseNumber1 extends AppCompatActivity {

    private ArrayAdapter<String> mobileNumberAdapter;
    private ArrayList<String> mobileNumbersList = new ArrayList<>();
    private ListView mobileNumberListView;

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

        ...

        mobileNumberListView = (ListView) findViewById(R.id.listViewNumberList);

        mobileNumbersList = getIntent().getStringArrayListExtra("mobileNumbers");
        if(mobileNumbersList != null) {
            mobileNumberAdapter = new ArrayAdapter<String>(this,
                                                           R.layout.list_item_numbers,
                                                           R.id.list_item_number_textview,
                                                           mobileNumbersList);
            mobileNumberListView.setAdapter(mobileNumberAdapter);
        }
    }
}


 类似资料:
  • 我正在尝试将数据从DialogFraank发送到创建此对话框的Fraank。在目标片段中,我使用以下代码: 我还在fragment实现了我的接口: 在DialogFragment中,我使用以下代码: 并发送数据: 我还试图从这个问题中使用onActivityResult(): 接收数据: 但我没有收到数据:(((

  • 致命异常:java.lang.nullpointerException:试图在com.myapp.apkofferQueueManagerIntentService.OnHandleIntent(SourceFile:71),android.app.IntentService$ServiceHandler.HandleMessage(IntentService.java:65),android.o

  • 我正在开发的一个应用程序允许用户自己读取确认短信的内容,输入验证码。对于使用早于Oreo(API 26)的操作系统的所有设备,BroadcastReceiver的实现工作正常,并允许正确接收SMS。在这个实现中,我的意思是将receiver对象放置在AndroidManifest中。 但是,从Oreo开始,必须显式地将BroadcastReceivers注册到适当的上下文。我对此执行如下: 在接收

  • Android清单的重要部分是: 为什么我的广播接收器没有收到意图?

  • 问题内容: 我正在尝试从servlet发送电子邮件。也不例外,但是我的帐户中没有收到电子邮件。我正在使用以下代码: } 我现在收到以下异常: 谁能告诉我为什么我没有收到电子邮件以及此代码有什么问题。提前致谢 问题答案: 阅读https://developers.google.com/appengine/docs/java/mail/overview 基本上停止在您的应用程序中包含任何JavaMai

  • 问题内容: 我正在建立一个客户-服务器项目。 我需要的是客户端发送一个字符串,例如“ Pendu”,服务器接收该字符串并将一个名为“ Pendu”的对象发送回客户端。 这是我的代码: 该类在包中定义: 我的问题是: 首先,我执行服务器,然后看到控制台中显示的内容。 然后我执行客户端,在控制台中,我收到如下消息: 同时,服务器端未显示任何新内容。 现在,我停止客户端,并显示服务器的所有其他消息: 当