@Override
public void onDestroy()
{
MessageService.this.stopSelf();
messageThread.isRunning = false;
System.exit(0);
super.onDestroy();
}
我在销毁方法里写上了这些代码,在其他Activity里关闭服务(stopService(new Intent(MainOptionActivity.this,MessageService.class));),试了很多代码都不行啊?
大神门 你们的见解??
public void onDestroy()
{
MessageService.this.stopSelf();
messageThread.isRunning = false;
System.exit(0);
super.onDestroy();
}
我在销毁方法里写上了这些代码,在其他Activity里关闭服务(stopService(new Intent(MainOptionActivity.this,MessageService.class));),试了很多代码都不行啊?
大神门 你们的见解??
解决方案 »
- 使用aChartEngineer没有图像显示
- android 4.0 没有actvity就不启动服务了,求解释
- android 启动servie
- 安卓2.2版本问题
- 请教android 做照相 上传。。最好有源码发我一份。。[email protected]
- 请教关于Tabs页面里的onResume()复写问题
- 模块编译/frameworks/ex/chips后生成的东西在哪儿?
- 求助,新建project 的gen中无法生成R.java,在网上找了各种方法都没有用
- 安卓4.2.2 调节音量的函数如何换成自己的代码(内详细)
- ant自动编译打包apk,部分功能不可用
- 注册一个收件箱监听器发生Exported receiver does not require permission 警告
- 已经新起线程,为何有android.os.NetworkOnMainThreadException异常
/**
* 从服务器端获取消息
*/
class MessageThread extends Thread
{
// 设置为false后,线程跳出循环并结束
public boolean isRunning = true; public void run()
{
while (isRunning)
{
try
{
// 得到消息内容
String serverMessage = getServerMessage(); if (serverMessage != null && !"".equals(serverMessage))
{
// 设置消息内容
contentView.setTextViewText(R.id.text, serverMessage);
// 为意图添加参数
messageIntent.putExtra("message", serverMessage);
// 将意图装入 延迟意图
messagePendingIntent = PendingIntent.getActivity(
MessageService.this, 0, messageIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
// 将延迟意图装入消息
messageNotification.contentIntent = messagePendingIntent;
messageNotificatioManager.notify(messageNotificationID,
messageNotification); // messageNotificatioManager.cancel(messageNotificationID-1);//新消息来后,消除之前的一条消息(只显示最新消息)
// 配置好下条消息的id号
messageNotificationID++;
}
// 休息10秒钟
Thread.sleep(TIME);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
我Log一下 发现 线程的运行布尔值 一直为true 我明明设置为假了 还有销毁方法一直没有运行呀?
public class MyService extends Service {@Override
public IBinder onBind(Intent intent) { return null;
}@Override
public void onCreate() {
Toast.makeText(getApplicationContext(), "MSG onCreate SERVICE", Toast.LENGTH_LONG).show();
super.onCreate();
}@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "MSG onStartCommand SERVICE", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}@Override
public void onDestroy() {
Toast.makeText(getApplicationContext(), "MSG STOP SERVICE", Toast.LENGTH_LONG).show();
super.onDestroy();
}}
测试这个服务
startService(new Intent(this, MyService.class)); new Timer().schedule(new TimerTask() { @Override
public void run() {
startService(new Intent(getApplicationContext(), MyService.class));
}
}, 5000); new Timer().schedule(new TimerTask() { @Override
public void run() {
stopService(new Intent(getApplicationContext(), MyService.class));
}
}, 10000);
在manifest中添加:
<service android:name=".MyService" />