我想用以下代碼擴展Activity
廣播一條烤麪包消息。但是廣播沒有被另一個Activity
收到,烤麪包沒有被顯示。有人能解決我的錯誤嗎?主要活動是SendBroadcast.java廣播接收器沒有收到廣播
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class SendBroadcast extends Activity {
public static String BROADCAST_ACTION =
"com.unitedcoders.android.broadcasttest.SHOWTOAST";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendBroadcast(View v) {
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
sendBroadcast(broadcast);
}
}
吐司顯示活動是ToastDisplay.java
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
public class ToastDisplay extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "received",
Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(SendBroadcast.BROADCAST_ACTION);
registerReceiver(receiver, filter);
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
}
和manifest.java是如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broad"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SendBroadcast"
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=".ToastReceiver" >
<intent-filter>
<action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST" />
</intent-filter>
</receiver>
</application>
</manifest>
好兄弟可以告訴我,我必須從清單中刪除該靜態廣播,並可以給我一個提示什麼將是我剩下的.java文件 – 2012-02-21 13:32:02
這取決於你想要做什麼。我已經向您展示了一個非常簡單的示例,說明如何顯示收到廣播後顯示的敬酒信息。你可以簡單地創建一個新項目並測試這個類。 – Yury 2012-02-21 13:36:28
首先簡單我想要動態顯示Toast – 2012-02-21 13:41:40