简体   繁体   中英

android my service does not start

NewActivity.class (it is actually Service. sorry for odd naming)

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"/>

The Service does not start without any error. I can not see any log in NewActivity. is my code wrong? Plz help.

onStartCommand() function call when you bind your service with some activity. If you do not want to bind, then put it onCreate() method

    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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM