您的位置:首页 > 移动开发 > Android开发

如何创建前台Android Service

2016-04-09 20:12 579 查看

如何创建前台Android Service

代码

public class LocationService extends Service {
@Override
public void onCreate() {
super.onCreate();
Resources res = getResources();
Notification.Builder builder = new Notification.Builder(getApplicationContext());
PendingIntent contentIndent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
builder .setContentIntent(contentIndent)
.setSmallIcon(R.mipmap.ic_launcher)//设置状态栏里面的图标(小图标)             
.setLargeIcon(BitmapFactory.decodeResource(res, R.mipmap.ic_launcher))//下拉下拉列表里面的图标(大图标)        
.setTicker("在哪儿正在运行中") //设置状态栏的显示的信息
.setWhen(System.currentTimeMillis())//设置时间发生时间
.setAutoCancel(true)//设置可以清除
.setContentTitle("This is ContentTitle")//设置下拉列表里的标题
.setContentText("this is ContentText");//设置上下文内容
Notification notification = builder.build();//获取一个Notification
startForeground(1,notification);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: