5

嗨朋友们,我无法将广播从一项活动发送到其他活动,请参阅下面的代码并提供帮助:

      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>
4

2 回答 2

8

由于当您发送广播时其他活动未运行,您将不会收到它。如果你想在活动没有运行时接收广播。在 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 的第二个 Activity

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>
于 2012-04-25T09:42:03.263 回答
1

如果您的 SendBroadcast 活动已创建,则将发送广播。

然后开始第二个活动,称为 ToastDisplay,并在 onResume 中注册 BroadcastReceiver。但这已经晚了,广播已经发送了,它不会留在系统中!

尝试发送粘性广播,例如:

sendStickyBroadcast(Intent)

或者在清单中声明广播接收器,但是您需要创建一个扩展广播接收器类的单独类,这不能被继承。

于 2012-04-25T09:42:22.220 回答