qq_26605117 2015-05-02 09:37 采纳率: 0%
浏览 5596

android 启动service出现问题,直接“很抱歉,已停止运行”

在activity的onResume()方法启动服务,直接“很抱歉,已停止运行”

protected void onResume() {
super.onResume();
if(isNetworkAvailable())
{
Log.e("login_resume", "+++++++++++++++++++++++");
Intent service = new Intent(this, GetMsgService.class);
startService(service);
}else {
toast(this);
}

我的service是下面的内容,主要是通过一个client启动读写线程,用于socket的传输:
import com.example.client.Client;
import com.example.client.ClientInputThread;
import com.example.client.MessageListener;
import com.example.tran.bean.TranObject;
import com.example.util.Constants;
import com.example.util.SharedPreferencesUtil;

import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class GetMsgService extends Service{
private MyApplication myApplication;
private SharedPreferencesUtil util;
private Client client;
private Context context = this;
private boolean isStart = false;// 是否与服务器连接上

@Override
public void onCreate() {
        // TODO Auto-generated method stub
    super.onCreate();
    myApplication = (MyApplication) this.getApplicationContext();
    client = myApplication.getClient();
    Log.e("service_oncreate", "++++++++++++++++++++");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
super.onStartCommand(intent, flags, startId);
isStart = client.start();
myApplication.setClientStart(isStart);
System.out.print(isStart);
Log.e("service", "+++++++++++++++++++++++++++++");
if(isStart)
{
ClientInputThread inputThread = client.getClientInputThread();
inputThread.setMessageListener(new MessageListener() {

          @Override
          public void Message(TranObject tranObject) {
            // TODO Auto-generated method stub
              Intent broadcast = new Intent();
              broadcast.setAction(Constants.ACTION);
              broadcast.putExtra(Constants.MSGKEY, tranObject);
              sendBroadcast(broadcast);
          }
    });
}
    return START_REDELIVER_INTENT;

}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}

  • 写回答

2条回答 默认 最新

  • bdmh 移动开发领域优质创作者 2015-05-02 10:49
    关注

    看你的错误输出日志,然后定位到错误代码行

    评论

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决