将字符串从服务发送到活动 [英] Send string from service to activity
问题描述
我正在尝试通过广播将字符串从服务发送到我的主要活动. 我在一些论坛中读到,有两种使用广播的方法.一种是在清单中注册活动,第二种方法是在本地活动中进行. 我想知道如何使用第二种方法.我曾尝试这样做,但不幸的是我没有成功.
I'm trying to send string from service to my main activity with broadcast. I have read in a few forums that there are 2 ways to use broadcast. One is to register the activity in the manifest and the second way is to do it on the activity, local. I would like to know how I can use the second way. I have tried to do it but unfortunately I did not succeed.
请告诉我我在做错什么.
Please tell me what I'm doing wrong.
服务代码
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String typemessage = data.getString("typemessage"); // typeMessage = 0 or 1 = lock or unlock
String datamessage = data.getString("datamessage"); // dataMessage = time and message that says lock or unlock
Log.d(TAG, "TypeMessage: " + typemessage);
Log.d(TAG,"DataMesaage:"+ datamessage);
Intent in = new Intent();
in.putExtra("TYPE",typemessage);
in.setAction("NOW");
sendBroadcast(in);
...
主要活动代码
public class MainActivity extends Activity implements SensorEventListener, Listen {
private BroadcastReceiver statusReceiver;
private IntentFilter mIntent;
Sensor accelerometer;
SensorManager sm;
TextView acceleration;
SendValues sv;
int counter3 = 0;
int counter5 = 0;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntent = new IntentFilter("NOW");
// this.registerReceiver(,new IntentFilter("status"));
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
sm.registerListener(this,accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
acceleration = (TextView) findViewById(R.id.sensorTxt);
statusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String type = intent.getStringExtra("message"); //get the type of message from MyGcmListenerService 1 - lock or 0 -Unlock
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Log.d(TAG, "Status: " + type);
if (type == "1") // 1 == lock
{
Toast.makeText(getApplication(), "Lock", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplication(), "UNLOCK", Toast.LENGTH_LONG).show();
}
}
};
}
@Override
protected void onResume()
{
super.onResume();
registerReceiver(statusReceiver,mIntent);
}
@Override
protected void onPause() {
if(mIntent != null) {
unregisterReceiver(statusReceiver);
mIntent = null;
}
super.onPause();
}
----------
好吧,我不知道自己在做什么错,还有另一个问题,我需要在
Well I don't know what I'm doing wrong, and another question, what do I need to put in the
在动作集中(我可以在此处放置任何字符串/动作,这是什么意思?动作是什么意思?我只想将字符串发送给主要活动,我应该做什么动作?")
In set Action("I can put here any string/action that I what? And what does it mean action? I just want to send string to the main activity, what action should I do"?)
推荐答案
LocalBroadcastManager是解决此类问题的最佳解决方案.我已经将LocalBroadcastManager实现为您现有的.这很容易.希望它能工作.您的代码将如下所示
LocalBroadcastManager is the best solution for solving this type of problem. I have implemented LocalBroadcastManager to your existing. Its very easy. Hope it will work. You code will look something like this
private BroadcastReceiver statusReceiver;
private IntentFilter mIntent;
Sensor accelerometer;
SensorManager sm;
TextView acceleration;
SendValues sv;
int counter3 = 0;
int counter5 = 0;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//mIntent = new IntentFilter("NOW");
// this.registerReceiver(,new IntentFilter("status"));
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
sm.registerListener(this,accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
acceleration = (TextView) findViewById(R.id.sensorTxt);
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String type = intent.getStringExtra("message"); //get the type of message from MyGcmListenerService 1 - lock or 0 -Unlock
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Log.d(TAG, "Status: " + type);
if (type == "1") // 1 == lock
{
Toast.makeText(getApplication(), "Lock", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplication(), "UNLOCK", Toast.LENGTH_LONG).show();
}
}
};
@Override
protected void onResume()
{
super.onResume();
//registerReceiver(statusReceiver,mIntent);
LocalBroadcastManager.getInstance(MainActivity.this).registerReceiver(broadcastReceiver, new IntentFilter("NOW"));
}
@Override
protected void onPause() {
if(mIntent != null) {
unregisterReceiver(statusReceiver);
mIntent = null;
}
super.onPause();
}
并通过类似这样的推送通知传递您的价值
And pass your value from push notification something like this
Intent in = new Intent();
in.putExtra("TYPE",typemessage);
in.setAction("NOW");
//sendBroadcast(in);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(in);
这篇关于将字符串从服务发送到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!