首页 新闻 会员 周边 捐助

android 的service一直启动不了,不知道为何?

0
悬赏园豆:100 [已解决问题] 解决于 2014-06-17 15:00
package Com.RuiZhi.Iscan;

import java.util.HashMap;
import java.util.Map;

import Com.RuiZhi.Logic.Task;

import Com.RuiZhi.Logic.MainService;


import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
import Com.RuiZhi.Logic.*;

public class Login extends Activity implements IScanActivity {

    
    EditText txtUser;
    EditText txtPwd;
    Button btnLogin;
    
    public ProgressDialog pd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        txtUser = (EditText)findViewById(R.id.txtUser);
        txtPwd = (EditText)findViewById(R.id.txtPwd);
        //init() ;
        btnLogin = (Button)findViewById(R.id.loginButton);
        
        btnLogin.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
                pd = new ProgressDialog(Login.this);
                pd.setMessage("正在登录...");
                pd.show();
                
                String name = txtUser.getText().toString();
                String pass = txtPwd.getText().toString();
                Map map = new HashMap();
                map.put("user", name);
                map.put("pass", pass);
                System.out.print(name);
                Task task = new Task(Task.TASK_LOGIN,map);
                
                MainService.newTask(task);
            }
        });    
        MainService.allActivity.add(this);
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        init();
    }

    @Override
    public void init() {
        // TODO Auto-generated method stub
        System.out.print("初始化操作");
                    
        try
        {
        //Com.RuiZhi.Logic.MainService
        Intent it = new Intent("Com.RuiZhi.Logic.MainService");
        startService(it);
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
        
        
        
    }

    @Override
    public void refresh(Object... args) {
        // TODO Auto-generated method stub
        
        pd.dismiss();
        //if(args[1]==null){
            //Toast.makeText(this, "登录失败", 1000).show();
        //}else{
        //    Toast.makeText(this, "登录成功", 3000).show();
             System.out.print("进入跳转方法");
            Intent it = new Intent();
            it.setClass(Login.this, Main.class);
            startActivity(it);
            finish();
        //}
    }
    
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
////        getMenuInflater().inflate(R.menu.activity_login, menu);
////        return true;
//    }

    
}
package Com.RuiZhi.Logic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import Com.RuiZhi.Logic.IScanActivity;//Logic.IWeiboActivity;
import Com.RuiZhi.Logic.Task;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class MainService extends Service implements Runnable {

    //public static Weibo weibo = new Weibo();
    public static List<Activity> allActivity = new ArrayList<Activity>();
    public static List<Task> allTask = new ArrayList<Task>();

    public static HashMap<Integer, BitmapDrawable> alluserIcon = new HashMap<Integer, BitmapDrawable>();
//    public static User nowUser;

    public boolean isrun = true;

    public static Activity getActivityByName(String name) {

        for (Activity ac : allActivity) {
            if (ac.getClass().getName().indexOf(name) >= 0) {
                return ac;
            }
        }
        return null;
    }

    //启动线程
    @Override
    public void onCreate() {
         System.out.print("进入到线程");
        // TODO Auto-generated method stub
        super.onCreate();
        Thread t = new Thread(this);
        t.start();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    public static void newTask(Task task) {
         System.out.print("进入到添加处任务处");
        allTask.add(task);
    }

    //  执行任务,业务逻辑调用,执行完后移出任务
    public void doTask(Task task) {

        Message mess = handler.obtainMessage();
        mess.what = task.getTaskId();
        System.out.println("#####----doTask-----");
        try {
            switch (task.getTaskId()) {
            case Task.TASK_LOGIN:                
             System.out.print("进入到关键流程");

                break;

            case Task.TASK_GET_TIMELINE:            
                break;
                
            case Task.TASK_GET_USER_ICON:
                break;

            case Task.TASK_NEW_WEIBO:
                break;
            }
        } catch (Exception e) {
        }

        handler.sendMessage(mess); // 
        allTask.remove(task); // 
    }

    // 侦听任务
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (isrun) {
            System.out.println("------RUN-----------");
            Task lastTask = null;
            if (allTask.size() > 0) {
                lastTask = allTask.get(0);
                doTask(lastTask);
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
            ;
        }
    }
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    // 更新UI
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            System.out.println("~~~~处理参数,来自其它界面的参数~~~~~~~~~");
            switch (msg.what) {
            case Task.TASK_LOGIN:
                System.out.print("afasdfsf");
                IScanActivity ia = (IScanActivity) getActivityByName("Login");
                ia.refresh(msg.what, msg.obj);
                break;

            case Task.TASK_GET_TIMELINE:
                break;
                
            case Task.TASK_GET_USER_ICON:                
                break;

            case Task.TASK_NEW_WEIBO:
                break;
            }
        }

    };

    public static void exitApp(Context con) {
        for (Activity ac : allActivity) {
            ac.finish();
        }
        //
        Intent it = new Intent("Com.RuiZhi.Logic.MainService");
        con.stopService(it);
    }

}
陈力的主页 陈力 | 初学一级 | 园豆:117
提问于:2012-07-18 14:21
< >
分享
最佳答案
0

检查一下你的manifest文件吧,加上<service name="Com.RuiZhi.Logic.MainService"></service> 应该就是这个问题,祝你好运

收获园豆:100
hxors | 菜鸟二级 |园豆:302 | 2013-03-09 15:01
其他回答(3)
0

有什么错误信息吗?日志什么的

artwl | 园豆:16736 (专家六级) | 2012-07-18 14:52
0

Intent.setclass方法呢

cnyao | 园豆:259 (菜鸟二级) | 2012-07-18 16:19
0

manifest文件配置了吗?

嗷桑 | 园豆:204 (菜鸟二级) | 2012-07-19 07:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册