创建Service
public class MyService extends Service {
public static final String TAG = "MyService" ;
@Override
public void onCreate () {
super .onCreate();
Log.d(TAG, "onCreate() executed" );
}
@Override
public int onStartCommand (Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand() executed" );
return super .onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy () {
super .onDestroy();
Log.d(TAG, "onDestroy() executed" );
}
@Override
public IBinder onBind (Intent intent) {
return null ;
}
}
生成notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification myNotify = new Notification();
notify1.setLatestEventInfo(this , "Notification Title" ,
"This is the notification message" , pendingIntent);
manager.notify(NOTIFICATION_FLAG, notify1);
示例
package com.android.yunshi.service;
import android.app.Service;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import com.android.yunshi.LogoActivity;
import com.android.yunshi.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
public class DailyYunChengTuiSong extends Service {
public Timer timer;
final String TAG = "com.yhf.dailyyunchengtuisong" ;
int CUSTOM_VIEW_ID = 1 ;
Calendar calendar;
Boolean isyunchengtuisong_Done;
@Override
public IBinder onBind (Intent intent) {
Log.i(TAG, "onBind" );
return null ;
}
@Override
public boolean onUnbind (Intent intent) {
Log.i(TAG, "onUnbind" );
return super .onUnbind(intent);
}
@Override
public void onRebind (Intent intent) {
super .onRebind(intent);
Log.i(TAG, "onRebind" );
}
@Override
public void onCreate () {
super .onCreate();
Log.i(TAG, "onCreate" );
timer = new Timer(true );
}
@Override
public void onDestroy () {
super .onDestroy();
Log.i(TAG, "onDestroy" );
}
@Override
public void onStart (Intent intent, int startId) {
super .onStart(intent, startId);
Log.i(TAG, "onStart" );
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10 );
calendar.set(Calendar.MINUTE, 0 );
calendar.set(Calendar.SECOND, 0 );
Date time = calendar.getTime();
timer.schedule(new TimerTask() {
public void run () {
int icon = R.drawable.yunshi360;
CharSequence tickerText = "运势360" ;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon,
tickerText, when);
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.yunshi_more_tuisong_notification);
contentView.setImageViewResource(R.id.image,
R.drawable.yunshi360);
contentView.setTextViewText(R.id.title, "每日运程" );
contentView.setTextViewText(R.id.text,
"记得开启运势360查看今日运程呦···" );
notification.contentView = contentView;
Intent notificationIntent = new Intent(
DailyYunChengTuiSong.this , LogoActivity.class);
PendingIntent contentIntent = PendingIntent
.getActivity(DailyYunChengTuiSong.this , 0 ,
notificationIntent, 0 );
notification.contentIntent = contentIntent;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults =Notification.DEFAULT_SOUND;
String ns = NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(CUSTOM_VIEW_ID, notification);
DailyYunChengTuiSong.this .stopSelf();
}
}, time);
}
}
注册Service
<service android:name ="com.android.yunshi.service.DailyYunChengTuiSong" >
<intent-filter >
<action android:name ="com.yhf.dailyyunchengtuisong" >
</action >
<category android:name ="android.intent.category.default" />
</intent-filter >
</service >
启动Service
intent.setAction ("com.yhf.dailyyunchengtuisong" )
startService(intent)