朋友们,我无法将广播从一个活动发送到其他活动请参阅下面的代码并提供帮助:
public class SendBroadcast extends Activity {
public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST";
/* }
});
}
public void sendBroadcast(){
Intent broadcast = new Intent("com.unitedcoders.android.broadcasttest.SHOWTOAST");
this.sendBroadcast(broadcast);
//startActivity(broadcast);
}
}
接收旁边代码:
public class ToastDisplay extends Activity {
private BroadcastReceiver mReceiver;
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("!!!!!!!InchooTutorial@@@@@@@$$$$","%%%%%%% msg_for_me");
ENT // String msg_for_me = intent.getStringExtra(“some_msg”); //记录我们的消息值 Log.i(“!!!!!!! InchooTutorial @@@@@@ $$$$”,“%%%%%%% msg_for_me”);
}
};
//registering our receiver
this.registerReceiver(mReceiver, intentFilter);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//unregister our receiver
this.unregisterReceiver(this.mReceiver);
}
}
Manifest.xml是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unitedcoders.android.broadcasttest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SendBroadcast"
android:label="@string/app_name">
<intent-filter>
nitedcoders.android.broadcasttest.SHOWTOAST" />
</application> </manifest>
答案 0 :(得分:6)
由于当您发送广播时其他活动未运行,您将不会收到它。 如果您希望即使活动未运行也要接收广播。在xml中声明它。
这是给你的代码。我希望这就是你想要的。
package com.pdd.Receiver;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ReceiverActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i =new Intent("com.pdd.receiver.myaction");
sendBroadcast(i);
}
}
接收者类
package com.pdd.Receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
//Intent i=new Intent(MyReceiver.class,Second.class);
Intent i=new Intent(arg0,Second.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(i);
}
}
显示Toast的第二个活动
package com.pdd.Receiver;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Toast.makeText(getApplicationContext(), "This is second activity", 5000).show();
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pdd.Receiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name="com.pdd.Receiver.MyReceiver">
<intent-filter>
<action android:name="com.pdd.receiver.myaction"></action>
</intent-filter>
</receiver>
<activity
android:name=".ReceiverActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Second"></activity>
</application>
</manifest>
答案 1 :(得分:1)
如果您的SendBroadcast活动已创建,则将发送广播。
然后启动第二个名为ToastDisplay的活动,并在onResume中注册BroadcastReceiver。但这是为了迟到,广播已经发送,它不会停留在系统中!
尝试发送像:
这样的粘贴广播sendStickyBroadcast(Intent)
或者在清单中声明broadcastreceiver但是你需要创建一个扩展BroadcastReceiver类的单独的类,这不能被继承。