为什么LocalBroadCastManager从IntentService发送的意图没有被Activity类中的BroadCastReceiver接收? [英] Why intent sent by LocalBroadCastManager from IntentService is not received by BroadCastReceiver within Activity class?
问题描述
ChooseLanguageFragment.java
ChooseLanguageFragment.java
Intent explicitGetNumberServiceIntentUSA = new Intent(getActivity(), GetNumberService.class);
explicitGetNumberServiceIntentUSA.putExtra("country", "USA");
getActivity().startService(explicitGetNumberServiceIntentUSA);
这就是GetNumberService的调用方式。
This is how the GetNumberService is called.
这是从GetNumberService.java传递arraylist的方式
This is how the arraylist is passed from 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);
。
GetNumberService完美地完成了它的工作。
GetNumberService does its job perfectly.
ChooseNumber1.java
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
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
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并且堆栈跟踪没有错误。我很震惊
Problem is, I am not able to receive the mobileNumbersIntent in ChooseNumber1.java or ChooseNumbersFragment.java. I think the issue is with BroadcastManager.java. If I debug, it goes to Handler.java and few other classes and finally mId=-1 and no error in stack trace. I am struck
PS:AndroidManifest.xml
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中的大量示例和答案后,我已经修改了很多代码。这是我的最新代码。看起来问题是使用intent-filter和/或使用action获取intent。请帮我。谢谢。
PS: I have modified my code a lot after going through tons of examples and answers in Stackoverflow. This is my latest code. Look like the issue is with intent-filter and/or getting intent using action. Please help me. Thanks.
推荐答案
所需的流程不需要广播和接收器设置。由于 ChooseNumber1
活动
将在服务
之后启动已完成其工作,您只需将 ArrayList
附加到用于启动的
。然后可以在 Intent
ChooseNumber1 onCreate()
中检索该列表,并使用 ListView
及其适配器
可以立即初始化,而不是等待广播。
The desired flow doesn't require a broadcast and Receiver setup. Since the ChooseNumber1
Activity
is to be started after the Service
has completed its work, you simply need to attach the ArrayList
to the Intent
used to start ChooseNumber1
. The list can then be retrieved in onCreate()
, and your ListView
and its Adapter
can be immediately initialized, rather than trying to wait for a broadcast.
例如,在服务中$列表汇编后c $ c>:
Intent mobileNumbersIntent = new Intent(this, ChooseNumber1.class);
mobileNumbersIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mobileNumbersIntent.putStringArrayListExtra("mobileNumbers", mobileNumberList);
startActivity(mobileNumbersIntent);
然后,在活动
中:
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);
}
}
}
这篇关于为什么LocalBroadCastManager从IntentService发送的意图没有被Activity类中的BroadCastReceiver接收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!