/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnQ=(Button)findViewById(R.id.bt); btnQ.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) {
// Start a Service Intent intent = new Intent(TestServiceActivity.this,GpsService.class); startService(intent);
}});
txtMsg = (TextView)this.findViewById(R.id.tv);
// BroadcastReceiver receiver = new UpdateReceiver(); IntentFilter filter = new IntentFilter(); // find a action. com.hoperun.gpsService is defined in GpsService class filter.addAction("com.hoperun.gpsService"); this.registerReceiver(receiver, filter);
}
private String strGpsValue=""; private UpdateReceiver receiver; public class UpdateReceiver extends BroadcastReceiver{
这个不太懂..不过程序在运行时居然在 logcat中出现了intent has leaked的错误... 虽然模拟器上照常运行,可是出现了这个错代码和肯定有问题...大家帮我看看呢
IntentFilter filter = new IntentFilter(); // find a action. com.hoperun.gpsService is defined in GpsService class filter.addAction("com.hoperun.gpsService"); this.registerReceiver(receiver, filter); 这是楼主的代码不过一般注册实在onResume中注册的 ,别忘了 根据业务逻辑 在onPause或onDestroy中取消注册
一个是直接调用service接口时获得返回值,这个不推荐,
另外常使用的是使用callback回调函数来获取service传回来的值;对于你下面说的“activity不可以作为BoradcastReceiver……”,你可以在activity中注册一个BoradcastReceiver来接收其他进程发来的广播。
private Button btnQ;
private TextView txtMsg;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); btnQ=(Button)findViewById(R.id.bt);
btnQ.setOnClickListener(new Button.OnClickListener(){ @Override
public void onClick(View v) {
// Start a Service
Intent intent = new Intent(TestServiceActivity.this,GpsService.class);
startService(intent);
}});
txtMsg = (TextView)this.findViewById(R.id.tv);
// BroadcastReceiver
receiver = new UpdateReceiver();
IntentFilter filter = new IntentFilter();
// find a action. com.hoperun.gpsService is defined in GpsService class
filter.addAction("com.hoperun.gpsService");
this.registerReceiver(receiver, filter);
}
private String strGpsValue="";
private UpdateReceiver receiver;
public class UpdateReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
strGpsValue = intent.getStringExtra("strGpsValue");
txtMsg.append(strGpsValue + "\n");
}
}}
这个不太懂..不过程序在运行时居然在 logcat中出现了intent has leaked的错误...
虽然模拟器上照常运行,可是出现了这个错代码和肯定有问题...大家帮我看看呢
IntentFilter filter = new IntentFilter();
// find a action. com.hoperun.gpsService is defined in GpsService class
filter.addAction("com.hoperun.gpsService");
this.registerReceiver(receiver, filter);
这是楼主的代码不过一般注册实在onResume中注册的 ,别忘了 根据业务逻辑 在onPause或onDestroy中取消注册
嗯由于没有在activity的onDestory事件中加入unRegisterService()logCat就报错了
十分感谢。。
我的思路是:在service中定义一个action,然后启动service的时候getaction,判断是否activity那里启动了我定义的这个action。如果是,则调用一个方法——也就是service那里设置一个参数值,然后通过intent启动activity。(我怀疑是不是就在这里出错了,可能是我理解能力比较差.........)在service中写的方法启动activity,让activity获取传递的值。
Intent intent2 =new Intent(DemoService.this,Demo.class);
intent2.putExtra("artistname", "zhoujielun");
startActivity(intent2);新手上路,请LZ指教!!!谢谢
如果可以的话请把该部分的Demo发到我邮箱——[email protected]或者加qq:397782562一起学习!万分感谢!~~~~~~~