如何将数据从Main Activity发送到BroadcastReceiver?

时间:2017-05-22 04:05:43

标签: android

我知道存在关于那个的线程,但是我不知道该怎么做,我只需要发送数据一个Broadcast,这就是代码

主要活动

 public void passData(){
    passIntent = new Intent(this,Notification_Reciever.class);
    path=String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath());
    passIntent.putExtra("KEY",path);
    sendBroadcast(passIntent);

}

广播

public class Notification_Reciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String action=intent.getAction();
    String value=intent.getStringExtra("KEY");
    if(Constantes.ACTION.PLAY_ACTION.equals(action)){
        actionPlay();
        Toast.makeText(context,""+value,Toast.LENGTH_SHORT).show();
    }

    if(Constantes.ACTION.PREV_ACTION.equals(action)){

    }

    if(Constantes.ACTION.NEXT_ACTION.equals(action)){

    }
}

清单

<receiver android:name=".Notification_Reciever">

        android:enabled="true"
        android:exported="true">

        <intent-filter>

            <action android:name="PLAY_ACTION"/>

        </intent-filter>

    </receiver>

始终为空

5 个答案:

答案 0 :(得分:0)

您没有对正在播放的意图设置操作。

passIntent = new Intent("action");

在你的情况下'passIntent = new Intent(Constantes.ACTION.PREV_ACTION);`或你要广播的任何一个动作。

您还可以使用intent.setAction(action)设置操作。

答案 1 :(得分:0)

function processInvites() {

    var cal = CalendarApp.getCalendarById("yourcalendar@google.com"),
        now = Date(),
        then = new Date(now.getTime() + (1000 * 60 * 60 * 24 * 7));

    cal.getEvents(now, then).forEach(function(event) {

        if (event.getMyStatus() == CalendarApp.GuestStatus.INVITED && event.getDescription() == "") {

            event.setMyStatus(CalendarApp.GuestStatus.NO);

        }

    });

}
  

你的清单文件应该是这样的......

Intent intent = new Intent();
intent.setAction("com.example.Broadcast");
intent.putExtra("data", "data");
sendBroadcast(intent);
  

您必须在活动中注册广播接收器..

<receiver android:name="MyReceiver" >
        <intent-filter>
            <action android:name="com.example.Broadcast" >
                </action>
        </intent-filter>
</receiver>
  

你的广播接收器将是这样的

IntentFilter filter = new IntentFilter("com.example.Broadcast");

MyReceiver receiver = new MyReceiver();
registerReceiver(receiver, filter);

答案 2 :(得分:0)

由于您在清单中定义了intent-filter,

<intent-filter>
    <action android:name="PLAY_ACTION"/>
</intent-filter>

您需要根据意图设置操作才能接收数据......

passIntent = new Intent(this,Notification_Reciever.class);
passIntent.setAction("PLAY_ACTION");
path = String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath());
passIntent.putExtra("KEY",path);
sendBroadcast(passIntent);

答案 3 :(得分:0)

你需要在这里设置动作。在清单中它已经注册

 public void passData(){
    passIntent = new Intent(this,Notification_Reciever.class);
    passIntent.setAction("PLAY_ACTION");  path=String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath();passIntent.putExtra("KEY",path);
    sendBroadcast(passIntent);
    }

答案 4 :(得分:0)

以下是广播的示例。这样做

Intent intent = new Intent("IncomingChat");
                intent.putExtra("visitor",    String.valueOf(eventParameters[0]));
                intent.putExtra("Action", "receivedincommingchats");
                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

并将其添加到您要接收广播的活动

   @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LocalBroadcastManager.getInstance(getActivity()).registerReceiver(IncomingBroadcastReceiver, new IntentFilter("IncomingChat"));
        }


private final BroadcastReceiver IncomingBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String Action = intent.getStringExtra("Action");
            switch (Action){
                case "receivedincommingchats":
                    String recVisit = intent.getStringExtra("visitor");

                    break;
                default:
                    break;
            }
        }
    };