android我的服务没有启动

时间:2015-12-22 04:58:01

标签: android service

NewActivity.class(实际上是Service。抱歉奇怪的命名)

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


import java.util.Date;


public class NewActivity extends Service {

    boolean flag;

    public IBinder onBind(Intent intent) {
        return null;
    }


    public void onCreate() {
        super.onCreate();

        Log.d("TAG", "Running");
        flag = true;
    }

    public int onStartCommand() {
        Thread t = new Thread(new Runnable() {
           public void run() {
               while(flag) {
                   try{
                       Thread.sleep(1000);
                   } catch(Exception e) {
                       e.printStackTrace();
                   }
                   Date d = new Date();
                   Log.i("MyTag", d.toString());
               }
           }
        });
        return START_STICKY;
    }

    public void onDestroy() {
        super.onDestroy();
        flag = false;
    }
}

MainActivity.class

public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        Button startBtn = new Button(this);
        Button isBtn    = new Button(this);

        startBtn.setText("Button");
        startBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startService(new Intent(MainActivity.this, NewActivity.class));
            }
        });



        isBtn.setText("check");
        isBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(isServiceRunningCheck())
                    Log.d("TAG", "Service is running.");

                else Log.d("TAG", "Service is not running.");
            }
        });

        layout.addView(startBtn);
        layout.addView(isBtn);

        setContentView(layout);
    }

    public boolean isServiceRunningCheck () {
        ActivityManager manager = (ActivityManager) this.getSystemService(Activity.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("com.example.zzing.test.NewActivity".equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}

的AndroidManifest.xml

<Service android:name=".NewActivity"/>

服务无法启动而没有任何错误。我在NewActivity中看不到任何日志。我的代码错了吗? Plz帮助。

1 个答案:

答案 0 :(得分:0)

使用某些活动绑定服务时,

onStartCommand()函数调用。 如果你不想绑定,那就把它放在onCreate()方法

    public class NewActivity extends Service {

    boolean flag;

    public IBinder onBind(Intent intent) {
        return null;
    }


    public void onCreate() {
        super.onCreate();

        Log.d("TAG", "Running");
        flag = true;
        Thread t = new Thread(new Runnable() {
           public void run() {
               while(flag) {
                   try{
                       Thread.sleep(1000);
                   } catch(Exception e) {
                       e.printStackTrace();
                   }
                   Date d = new Date();
                   Log.i("MyTag", d.toString());
               }
           }
        });
    }

    public int onStartCommand() {
        return START_STICKY;
    }

    public void onDestroy() {
        super.onDestroy();
        flag = false;
    }
}