【问题标题】:Why intent sent by LocalBroadCastManager from IntentService is not received by BroadCastReceiver within Activity class?为什么 LocalBroadCastManager 从 IntentService 发送的意图没有被 Activity 类中的 BroadCastReceiver 接收?
【发布时间】:2016-09-06 07:33:08
【问题描述】:

选择LanguageFragment.java

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

这就是调用 GetNumberService 的方式。

这是从 GetNumberService.java 传递数组列表的方式

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 完美地完成了它的工作。

选择Number1.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 中的大量示例和答案后,我对代码进行了很多修改。这是我最新的代码。看起来问题出在意图过滤器和/或使用操作获取意图。请帮我。谢谢。

【问题讨论】:

  • 使用 getView().findViewById();
  • 也发布您的完整堆栈跟踪。将此行 ArrayAdapter&lt;String&gt; mobileNumberAdapter 移动到 ListView mobileNumberListView 下方,然后移动 setAdapter
  • 我猜这不是 NPE,因为 findViewById()ListView 而是 NPE,因为您将空数组传递给 ArrayAdapter。发布你的 NPE 的堆栈跟踪。
  • @MikeM.Hi Mike,如果你把它作为答案,我想选择你的作为工作解决方案。

标签: java android android-layout listview android-fragments


【解决方案1】:

所需的流程不需要广播和接收器设置。由于ChooseNumber1 Activity 是在Service 完成其工作后启动的,您只需将ArrayList 附加到用于启动IntentIntent 上。然后可以在onCreate() 中检索该列表,并且可以立即初始化您的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);
        }
    }
}

【讨论】:

    【解决方案2】:

    您的findViewById() 很好,并且返回一个非空值。

    问题出在堆栈跟踪中的适配器上:您传递给它的mobileNumbersList 列表在分配适配器时为空。 ArrayAdapter 需要一个非空数组。

    (代码还有许多其他问题 - 这只是 NPE。)

    【讨论】:

    • 由于我在 ChooseNumbers.java 中将 mobileNumberList 声明为静态,并且在 BroadCastReceiver 中填充了 mobileNumberList,ChooseNumbersFragment.java 中的 mobileNumberList 不应该具有在 ChooseNumbers.java 中填充的值吗?
    • 广播接收器不一定会在您的片段实例化之前触发。
    • 1) ChooseNumbers.java 不会在 CHooseNumbersFragment.java 之前先执行吗?
    • 2) 我可以将IntentFilter filter = new IntentFilter(); filter.addAction(GetNumberService.MOBILE_NUMBER_LIST_PASS_ACTION); LocalBroadcastManager bm = LocalBroadcastManager.getInstance(this); bm.registerReceiver(mBroadcastReceiver, filter); 移动到ChooseNumberFragments.java 吗?这是首先填充 mobileNumberList 的解决方案吗?
    • "广播接收器不一定会在片段实例化之前触发。"在实例化片段之前,如何执行 BroadcastReceiver 并首先填充 mobileNumberList?
    【解决方案3】:

    我建议检查一下这个简单的代码是否有效:

    内部活动:

    @Override
          protected void onResume() {
            super.onResume();
            IntentFilter intFilt = new IntentFilter(MYBROADCASTINGSTRING);
            registerReceiver(updbr,intFilt);
          }
        @Override
          protected void onPause() {
            super.onPause();
            unregisterReceiver(updbr);
          }
    
    
            private BroadcastReceiver updbr = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent2) {
                        try {
    Log.d("MYLOG", "gotcha!");
                        } catch (Exception e) {
    
                        }
                    }
                };
    

    在捕捉的同时在服务中进行广播:

    Intent intentBR = new Intent(MYBROADCASTINGSTRING);
            intentBR.putExtra(somestring, "teststring");
            sendBroadcast(intentBR);
    

    MYBROADCASTINGSTRING 是一些常量。例如。 public final static String MYBROADCASTINGSTRING = "MYBROADCASTINGSTRING"; 不需要改进AndroidManifest.xml。它有效吗?

    【讨论】:

      【解决方案4】:

      当您发送广播时,您正在传递一个像这样的额外内容

      mobileNumbersIntent.putStringArrayListExtra("mobileNumbers", mobileNumberList);
      

      当您在 BroadcastReceiver 中读取额外内容时,您正在这样做。

      mobileNumbersList = intent.getStringArrayListExtra("mobileNumberList");
      

      您应该使用相同的密钥来获取附加信息。为此,我建议在 IntentService 中使用常量。

      public static final string EXTRA_MOBILE_NUMBERS = "mobile_numbers";
      

      然后你可以做这样的事情:

      mobileNumbersIntent.putStringArrayListExtra(MyIntentService.EXTRA_MOBILE_NUMBERS, mobileNumberList);
      

      mobileNumbersList = intent.getStringArrayListExtra(MyIntentService.EXTRA_MOBILE_NUMBERS);
      

      【讨论】:

        【解决方案5】:

        为了处理来自 IntentService 的结果,请使用 ResultReciver 而不是广播接收器。

        这样,

        第 1 步: 在 Activity 中使用 ResultReceiver 创建结果处理程序

        public ResultReceiver downloadReceiver = new ResultReceiver(new Handler()) {
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                super.onReceiveResult(resultCode, resultData);
                //Broadcast /send the result code in intent service based on your logic(success/error) handle with switch
                switch (resultCode) {
                    case 1: {
                        //Get the resultData and do your logic here
                        //Update your list view item here
                        break;
                    }
                    case 2: {
                        //Get the resultData and do your logic here
                        //Update your list view item here
                        break;
                    }
                }
            }
        }; 
        

        第 2 步:将结果处理程序放入 Intent 中并启动 Intent 服务

        Intent intent = new Intent(getContext(), DownloadService.class);
        intent.putExtra("receiver",downloadReceiver);
        

        第 3 步:在服务类中处理意图(获取结果处理程序的值)

        final ResultReceiver receiver;
        
        @Override
        protected void onHandleIntent(Intent intent) {
            receiver = intent.getParcelableExtra("receiver");
        }
        

        第 4 步:将数据发送/广播到结果处理程序

        //based on your logic, change the result code and data
        
        //Add your result data (success scenario)
        Bundle bundle = new Bundle();
        bundle.putString("result","Some text");
        receiver.send(1,bundle);
        
        //Add your result data (failure scenario)
        receiver.send(0,bundle);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-31
          • 1970-01-01
          • 2016-10-06
          相关资源
          最近更新 更多