我試圖在2個活動之間廣播意圖。如下圖所示意向廣播沒有收到的活動
mFiltersView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
CameraFiltersFactory effect = (CameraFiltersFactory) v.findViewById(R.id.filteredImage).getTag();
final Intent previewIntent = new Intent(FiltersActivity.this, PreviewActivity.class);
startActivityForResult(previewIntent, 0);
effect.save(new GPUImage.OnPictureSavedListener() {
@Override public void onPictureSaved(Uri uri) {
final Intent ImageIntent = new Intent();
ImageIntent.setData(uri);
ImageIntent.setAction("com.test.FILTER_RENDERED");
sendBroadcast(ImageIntent);
}
});
在PreviewActivity我需要註冊所述接收器和使用IntentFiter趕上意圖,並檢查它
意圖是從FilterActivity發送到PreviewActivity。
public class PreviewActivity extends Activity {
ReceiveMessages myReceiver = null;
Boolean myReceiverIsRegistered = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myReceiver = new ReceiveMessages();
if (!myReceiverIsRegistered) {
// registerReceiver(myReceiver, new IntentFilter());
registerReceiver(myReceiver, new IntentFilter("com.test.FILTER_RENDERED"));
myReceiverIsRegistered = true;
}
....
@Override
protected void onResume(){
super.onResume();
if (!myReceiverIsRegistered) {
registerReceiver(myReceiver, new IntentFilter("com.test.FILTER_RENDERED"));
myReceiverIsRegistered = true;
}
}
@Override
protected void onStop() {
super.onStop();
if (myReceiverIsRegistered) {
unregisterReceiver(myReceiver);
myReceiverIsRegistered = false;
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Intent mNewIntent = intent;
Bundle bundle = null;
String fragment = null;
if (intent != null) {
mImagePreviewUri = mNewIntent.getData();
if(mImagePreviewUri!=null) {
mCameraActionsView.setEnabled(true);
mImagePreview.setImageURI(mImagePreviewUri);
}
}
}
public class ReceiveMessages extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(action.equalsIgnoreCase("com.test.FILTER_RENDERED")){
if (intent != null) {
mImagePreviewUri = intent.getData();
if(mImagePreviewUri!=null) {
mCameraActionsView.setEnabled(true);
mImagePreview.setImageURI(mImagePreviewUri);
}
}
}
}
}
}
不幸的是,其意圖是永遠不會被活動<
任何想法得到爲什麼,的onReceive沒有被觸發?
有沒有一種方法可以確保新活動在發送意圖之前開始?因爲在我的代碼發送廣播之前,我開始了這個活動。 Pleaas檢查更新後的描述 – Seb
註冊是在onCreate上完成的onResume on onResume – Seb
儘管接收器是在onCreate()中初始化的。在onCreate()和OnResume()上完成註冊。看到你的代碼,看起來有一些設計問題。無論如何,要回答你以前的問題,如果你想傳遞一些信息給PreviewActivity,爲什麼不把它作爲額外的傳遞。在onPictureSaved內部創建previewIntent,並從那裏添加附加內容和startActivityForResult?我沒有看到發送廣播的需要。 – takesavy