@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));),试了很多代码都不行啊?
大神门 你们的见解??
解决方案 »
- Android 怎么向打开的网页中自动填充用户名和密码?
- 小弟求安卓开发入门视频,谢谢啦
- 求解
- 找个朋友帮忙下在新浪微博sdk
- 如何使应用在执行时先搜索包内的类,再搜索framework中的类?
- 关于使用handle产生The Application has stopped unexpectedly的问题
- 启动一个程序,延迟2秒进去,跳过LOGO
- Android中viewgroup中控件置顶问题
- Rect计算问题
- 有人遇到过这种问题没?
- 注册一个收件箱监听器发生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" />