Start service in Android

当某个活动开始时,我想调用一个服务:

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结尾处调用。我已经调试过了,没有抛出异常。

知道吗?

313803 次浏览

也许您没有在您的清单服务,或者它没有一个 <intent-filter>匹配您的行动。检查 LogCat (通过 adb logcat、 DDMS 或 Eclipse 中的 DDMS 透视图)应该会出现一些可能有帮助的警告。

More likely, you should start the service via:

startService(new Intent(this, UpdaterServiceManager.class));
startService(new Intent(this, MyService.class));

仅仅写这一行对我来说是不够的。服务仍然没有工作。一切都只有在登记服务清单后工作

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >


...


<service
android:name=".MyService"
android:label="My Service" >
</service>
</application>

Java code for 开始 服务:

Start service from Activity:

startService(new Intent(MyActivity.this, MyService.class));

碎片开始服务:

getActivity().startService(new Intent(getActivity(), MyService.class));

我的服务 java :

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


}

Define this Service into Project's Manifest File:

旅客名单文件中添加以下标记:

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

别忘了清单

<service android:enabled="true" android:name=".MyService.class" />
Intent serviceIntent = new Intent(this,YourActivity.class);


startService(serviceIntent);

在舱单中增加服务

<service android:enabled="true" android:name="YourActivity.class" />

运行服务的奥利奥和更大的设备用于地面服务,并显示通知用户

or use geofencing service for location update in background 参考文献 Http://stackoverflow.com/questions/tagged/google-play-services