android服务在哪里启动,在Android中启动服务

I want to call a service when a certain activity starts. So, here's the Service class:

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

}

}

And here is how I call it:

Intent serviceIntent = new Intent();

serviceIntent.setAction("cidadaos.cidade.data.UpdaterServiceManager");

startService(serviceIntent);

The problem is that nothing happens. The above code block is called at the end of the activity's onCreate. I already debugged and no exception is thrown.

Any idea?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值