我不知道为什么我的接收器没有收到任何东西。 我想在两个活动之间发送广播。 tv1没有显示“msg”。
这是代码
发送:
Intent intent = new Intent(action);
intent.putExtra("msg", "a");
sendBroadcast(intent);
startActivity(new Intent(MainActivity.this,sec.class));
接收者:
IntentFilter intentFilter = new IntentFilter(MainActivity.action);
registerReceiver(receiver, intentFilter);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
tv1.setText(intent.getExtras().getString("msg"));
}
};
答案 0 :(得分:1)
你可以这样实现:
package com.example.broadcastrecieverdemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private InternalReciever internalReciever;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
internalReciever = new InternalReciever();
IntentFilter filter = new IntentFilter("kishan");
registerReceiver(internalReciever, filter);
findViewById(R.id.btnSendBroadcast).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.example.broadcastrecieverdemo.CUSTOM_INTENT");
sendBroadcast(intent);
}
});
findViewById(R.id.btnInternalSendBroadcast).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("kishan");
sendBroadcast(intent);
}
});
}
@Override
protected void onDestroy() {
unregisterReceiver(internalReciever);
super.onDestroy();
}
}
class InternalReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Inernal Broadcast recieved",
Toast.LENGTH_SHORT).show();
}
}
<强> Reciever.java 强>
package com.example.broadcastrecieverdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Reciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast recieved", Toast.LENGTH_SHORT)
.show();
}
}
<强>清单强>
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcastrecieverdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".Reciever" >
<intent-filter>
<action android:name="com.example.broadcastrecieverdemo.CUSTOM_INTENT" >
</action>
</intent-filter>
</receiver>
</application>
<强> activity_main.xml中强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Broad cast receiver"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/btnSendBroadcast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="21dp"
android:gravity="center"
android:text="Send" />
<Button
android:id="@+id/btnInternalSendBroadcast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/btnSendBroadcast"
android:layout_marginTop="21dp"
android:gravity="center"
android:text="Send Internal" />