繁体   English   中英

无法将广播从活动发送到其他人:android

[英]unable to send Broadcast from activity to other: android

我从一个活动发送广播接收到其他活动有问题..它不起作用我的代码在下面..pls引用它..

发送类是:

    public class SendBroadcast extends Activity {
public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST";

/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        sendBroadcast();
        }

    });
}




  public void sendBroadcast(){

    Intent broadcast = new Intent();
    broadcast.setAction("com.unitedcoders.android.broadcasttest.SHOWTOAST");
    sendBroadcast(broadcast);
}

}

接收类是:

  public class ToastDisplay extends Activity {

private BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("asdasd","sdasdasd");
        // TODO Auto-generated method stub
        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();
}

}

清单文件为::::

 <?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" />

  <application android:icon="@drawable/icon" 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>
  <activity android:name=".ToastDisplay">

    <intent-filter>
        <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST">  </action>
    </intent-filter>
  </activity>

  </application>
  </manifest>

您尚未在清单上注册接收者:注册为

<receiver android:name="receiver">
<intent-filter>
<action
android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
</intent-filter>
</receiver>

如果要将数据从一个活动广播到另一个活动,只需使用Intent。 在第二个活动的onDestroy方法中,要从其发送数据的位置,创建Intent对象并作为额外的intent广播数据,然后在broadcastReceiver类的onReceive()方法上使用intent.getExtra()方法。

有关更多详细信息:请遵循本教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM