在Android中启动服务
我想在某个活动开始时调用服务。下面是服务类:在Android中启动服务,android,service,android-activity,Android,Service,Android Activity,我想在某个活动开始时调用服务。下面是服务类: public class UpdaterServiceManager extends Service { private final int UPDATE_INTERVAL = 60 * 1000; private Timer timer = new Timer(); private static final int NOTIFICATION_EX = 1; private NotificationManager n
public class UpdaterServiceManager extends Service {
private final int UPDATE_INTERVAL = 60 * 1000;
private Timer timer = new Timer();
private static final int NOTIFICATION_EX = 1;
private NotificationManager notificationManager;
public UpdaterServiceManager() {}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// Code to execute when the service is first created
}
@Override
public void onDestroy() {
if (timer != null) {
timer.cancel();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startid) {
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
int icon = android.R.drawable.stat_notify_sync;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
notificationManager.notify(NOTIFICATION_EX, notification);
Toast.makeText(this, "Started!", Toast.LENGTH_LONG);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Check if there are updates here and notify if true
}
}, 0, UPDATE_INTERVAL);
return START_STICKY;
}
private void stopService() {
if (timer != null) timer.cancel();
}
}
我这样称呼它:
Intent serviceIntent = new Intent();
serviceIntent.setAction("cidadaos.cidade.data.UpdaterServiceManager");
startService(serviceIntent);
问题是什么也没发生。上述代码块在活动的onCreate
结束时调用。我已经调试过了,没有抛出异常
有什么想法吗?可能您的清单中没有该服务,或者它没有与您的操作匹配的
。检查LogCat(通过Eclipse中的adb LogCat、DDMS或DDMS透视图)时,应该会出现一些可能有用的警告
更可能的情况是,您应该通过以下方式启动服务:
startService(new Intent(this, UpdaterServiceManager.class));
仅仅写这一行对我来说是不够的。服务仍然不起作用。只有在舱单上注册服务后,一切才开始工作
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
...
<service
android:name=".MyService"
android:label="My Service" >
</service>
</application>
...
启动服务的Java代码服务:
startService(new Intent(MyActivity.this, MyService.class));
getActivity().startService(new Intent(getActivity(), MyService.class));
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static String TAG = "MyService";
private Handler handler;
private Runnable runnable;
private final int runTime = 5000;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(runnable, runTime);
}
};
handler.post(runnable);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
if (handler != null) {
handler.removeCallbacks(runnable);
}
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(TAG, "onStart");
}
}
从活动启动服务:
startService(new Intent(MyActivity.this, MyService.class));
getActivity().startService(new Intent(getActivity(), MyService.class));
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static String TAG = "MyService";
private Handler handler;
private Runnable runnable;
private final int runTime = 5000;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(runnable, runTime);
}
};
handler.post(runnable);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
if (handler != null) {
handler.removeCallbacks(runnable);
}
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(TAG, "onStart");
}
}
从片段启动服务:
startService(new Intent(MyActivity.this, MyService.class));
getActivity().startService(new Intent(getActivity(), MyService.class));
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static String TAG = "MyService";
private Handler handler;
private Runnable runnable;
private final int runTime = 5000;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(runnable, runTime);
}
};
handler.post(runnable);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
if (handler != null) {
handler.removeCallbacks(runnable);
}
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(TAG, "onStart");
}
}
MyService.java:
startService(new Intent(MyActivity.this, MyService.class));
getActivity().startService(new Intent(getActivity(), MyService.class));
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static String TAG = "MyService";
private Handler handler;
private Runnable runnable;
private final int runTime = 5000;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(runnable, runTime);
}
};
handler.post(runnable);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
if (handler != null) {
handler.removeCallbacks(runnable);
}
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(TAG, "onStart");
}
}
将此服务定义到项目的清单文件中:
在清单文件中添加以下标记:
<service android:enabled="true" android:name="com.my.packagename.MyService" />
完成了我想让它更有活力
Class<?> serviceMonitor = MyService.class;
private void startMyService() { context.startService(new Intent(context, serviceMonitor)); }
private void stopMyService() { context.stopService(new Intent(context, serviceMonitor)); }
Class-serviceMonitor=MyService.Class;
私有void startMyService(){context.startService(新意图(context,serviceMonitor));}
私有void stopMyService(){context.stopService(新意图(context,serviceMonitor));}
别忘了舱单
<service android:enabled="true" android:name=".MyService.class" />
在manifist中添加服务
<service android:enabled="true" android:name="YourActivity.class" />
用于在oreo和更大的设备上运行服务,用于地面服务并向用户显示通知
或者在后台使用地理围栏服务进行位置更新
参考文献
当我将活动和服务放在同一个包中时,它能在多大程度上提高性能?以前从未听说过。也许他们指的是非常模糊的松散意义上的性能,而不是运行速度?小心计时器-当您的服务关闭以释放资源时,此计时器不会在服务重新启动时重新启动。没错,
START\u STICKY
将重新启动服务,但只调用onCreate,并且不会重新初始化计时器变量。您可以使用START\u REDELIVER\u INTENT
、报警服务或API 21作业调度器来解决此问题。如果您忘记了,请确保您已使用应用程序标记中的
在android清单中注册了该服务。如何调试?从未调用过我的服务,我的调试没有显示任何内容在任何地方添加大量的Log.e标记:在启动服务之前,服务意图的结果,在它将运行的服务类中(onCreate、onDestroy、任意和所有方法)。它适用于android sdk 26+上的我的应用程序,但不适用于android sdk 25或更低版本。有什么解决办法吗?@MahidulIslam:我建议你问一个单独的堆栈溢出问题,您可以提供一个更详细的解释您的问题和症状的示例。@Commonware我已经问了一个问题,这是:-了解Android服务的所有信息的最佳示例,很抱歉,我的自动化搜索到此结束。在我的应用程序中使用了Manifest和startService,它就像一个符咒。非常感谢。