如何在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();