Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中使用后台服务_Android_Android Service - Fatal编程技术网

如何在android中使用后台服务

如何在android中使用后台服务,android,android-service,Android,Android Service,我将在我的应用程序中使用后台服务,我正在使用一些代码,但它不起作用 public class MyService extends Service { String tag="TestService"; @Override public void onCreate() { super.onCreate(); Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();

我将在我的应用程序中使用后台服务,我正在使用一些代码,但它不起作用

  public class MyService extends Service {


   String tag="TestService";
    @Override
    public void onCreate() {
   super.onCreate();
   Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();      
   Log.i(tag, "Service created...");
     }

    @Override
    public void onStart(Intent intent, int startId) {      
   super.onStart(intent, startId);  
   Log.i(tag, "Service started...");
      }
    @Override
   public void onDestroy() {
   super.onDestroy();
   Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
  }

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

请注意,
onStart()
已被弃用,请改用
onStart命令()

你如何开始你的服务?您可能应该从您的活动开始,使用:

startService(new Intent(this, MyService.class));   
此外,在服务中,使用:

Toast.makeText(getApplicationContext(), "...", Toast.LENGTH_SHORT).show();
而不是:

Toast.makeText(this, "...", Toast.LENGTH_SHORT).show();