在我的一项活动中,我有一个ListView
和两个Buttons
名为"启动服务"和#34;停止服务"
在"开始服务"。 StartServiceActivity.class
调用extends Service
,并使用onStartCommand()
方法sendBroadcast
调用ServiceSecondActivity
并传递一些数据。
我的第三课extends BroadcastReceiver
Toasts
接收消息并使用BroadcastReceiver
现在我要做的是从中获取收到的数据
ListView
并在BroadcastReceiver
上显示。我已经完成了代码
但是我不知道我在哪里放置Button
对象的代码
它运行,
我无法通过"停止服务"停止我的服务public class ServiceActivity extends Activity implements OnClickListener {
ListView lstv;
Button bstart, bstop;
private BroadcastReceiver broadcastReceiver;
String data[];
private IntentFilter filter;
Messenger mService;
Boolean mBound;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.service_layout);
bstart = (Button) findViewById(R.id.bStart);
bstop = (Button) findViewById(R.id.bStop);
bstart.setOnClickListener(this);
bstop.setOnClickListener(this);
lstv=(ListView)findViewById(R.id.lst);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
data = new String[] { intent.getStringExtra("name"),
intent.getStringExtra("message"),
intent.getStringExtra("TimeStamp") };
ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data);
lstv.setAdapter(adp);
}
};
registerReceiver(broadcastReceiver, filter);
Toast.makeText(getApplicationContext(), "data : "+ data, Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(broadcastReceiver);
}
@Override
public void onClick(View arg0) {
//registerReceiver(broadcastReceiver, filter);
switch (arg0.getId()) {
case R.id.bStart:
Intent i = new Intent(getApplicationContext(),
StartServiceActivity.class);
startService(i);
break;
case R.id.bStop:
Intent ii = new Intent(getApplicationContext(),
StartServiceActivity.class);
//unregisterReceiver(broadcastReceiver);
stopService(ii);
break;
default:
break;
}
}
}
。
1)第一项活动
public class StartServiceActivity extends Service{
ListView lvl;
private PendingIntent pendingIntent;
int count=1;
Long stm;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(getApplicationContext(), "Service Created", Toast.LENGTH_SHORT).show();
}
@Override
public Context getApplicationContext() {
// TODO Auto-generated method stub
return super.getApplicationContext();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_SHORT).show();
Notification notification=new Notification(R.drawable.ic_launcher, "Service " + count++, System.currentTimeMillis());
Intent inte=new Intent(getApplicationContext(), ServiceActivity.class);
PendingIntent penIn=PendingIntent.getActivity(getApplicationContext(), 0, inte, 0);
notification.setLatestEventInfo(getApplicationContext(), "Service ", "Service is running", penIn);
startForeground(20, notification);
}
public int onStartCommand(Intent intent, int flags, int startId) {
stm=System.currentTimeMillis()+ 5*1000;
while (System.currentTimeMillis()< stm) {
Intent ii=new Intent(getApplicationContext(), ServiceSecondActivity.class);
ii.putExtra("name", "abc");
ii.putExtra("message", "Number"+ count++);
ii.putExtra("TimeStamp", "TimeStamp :"+stm);
ii.setAction("demo.example.app_5_1_15.CUSTOM_INTENT");
sendBroadcast(ii);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stu
stopForeground(true);
stopSelf();
super.onDestroy();
Toast.makeText(getApplicationContext(), "Service Stopped", Toast.LENGTH_SHORT).show();
}
}
2)班级wchich扩展服务
public class ServiceSecondActivity extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
CharSequence csName=intent.getCharSequenceExtra("name");
CharSequence csMsg=intent.getCharSequenceExtra("message");
CharSequence csTime=intent.getCharSequenceExtra("TimeStamp");
Toast.makeText(context, "msg : "+csName + " : " + csMsg +" : " + csTime, Toast.LENGTH_SHORT).show();
}
}
3)扩展BroadcastReceiver的类
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.app.ContextImpl.stopServiceAsUser(ContextImpl.java:1389)
at android.app.ContextImpl.stopService(ContextImpl.java:1359)
at android.content.ContextWrapper.stopService(ContextWrapper.java:455)
at demo.example.app_5_1_15.ServiceActivity.onClick(ServiceActivity.java:120)
at android.view.View.performClick(View.java:4202)
at android.view.View$PerformClick.run(View.java:17340)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
错误日志
{{1}}
答案 0 :(得分:0)
(我不懂英文。)
1.我不知道,但如果您想发送数据,可以使用Message in android
。
2.服务是singelton所以我可以通过再次启动来阻止它,但不同的意图是单独停止它。
按钮:
Intent ii = new Intent(getApplicationContext(),
StartServiceActivity.class);
ii.putExtras("stop",1);
startService(ii);
在您的服务中:
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.hasExtra("stop") ) {
//stop it now
stopForeground(true);
stopSelf();
}else{
//.............
}
}