我的 [0] => Array
(
[name] => Cash crops
)
[1] => Array
(
[name] => Pulse crops
)
[2] => Array
(
[name] => Oil seed crops
)
[3] => Array
(
[name] => Cereal Crops
)
中有BroadcastReceiver
个班级。我想从接收器类开始Activity
。我可以从写入接收器的同一Fragment
调用它吗?
答案 0 :(得分:0)
是的,您可以使用LocalBroadCastManger
来完成import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class LocalBroadcastExampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.log_list);
Button buttonStartService = (Button)findViewById(R.id.button_ok);
buttonStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Register MessageService in Manifest to work
startService(new Intent(LocalBroadcastExampleActivity.this, MessageService.class));
}
});
}
@Override
protected void onPause() {
// Unregister since the activity is paused.
LocalBroadcastManager.getInstance(this).unregisterReceiver(
mMessageReceiver);
super.onPause();
}
@Override
protected void onResume() {
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("custom-event-name"));
super.onResume();
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
public class MessageService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
sendMessage();
return super.onStartCommand(intent, flags, startId);
}
// Send an Intent with an action named "custom-event-name". The Intent
// sent should
// be received by the ReceiverActivity.
private void sendMessage() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("custom-event-name");
// You can also include some extra data.
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
}
答案 1 :(得分:0)
尝试这样的事情......如果您使用自定义广播接收器,则将此类替换为接收者类的广播接收。
public class Demo extends AppCompatActivity {
private final IntentFilter filter = new IntentFilter();
private BroadcastReceiver networkStateReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onResume() {
super.onResume();
// Defining broadcast receiver in onResume()
networkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Do what you want
}
};
// Registering receiver with intent filter, here intent filter can be changed
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(networkStateReceiver, filter);
}
@Override
protected void onPause() {
super.onPause();
// Unregister receiver in onStop to avoid any runtime exception
unregisterReceiver(networkStateReceiver);
}
}