package com.WTFly.Service;
import com.WTFly.infoqueue.MainActivity;
import com.WTFly.infoqueue.R;
import android.R.string;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;
public class InfoService extends IntentService
{
public static String TAG = "InfoService";
public InfoService()
{
super("InfoService");
}
@Override
protected void onHandleIntent(Intent intent)
{
Log.i(TAG, "开始下载");
ShowNotification(false);
try
{
Thread.sleep(10000);
ShowNotification(true);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(TAG, "程序下载完毕");
}
private void ShowNotification(boolean finish)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification;
Intent intent = new Intent(this, MainActivity.class);
// 设置在下拉列表中的信息,其中PendingIntent用于设置,Notification项被按下后,发送的Intent.此处为打开MainActivity
// 该语句的作用是定义了一个不是当即显示的activity,只有当用户拉下notify显示列表,并且单击对应的项的时候,才会触发系统跳转到该activity.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (!finish)
{
notification = new Notification(R.drawable.title, "开始下载", System.currentTimeMillis());
// 在此处设置在notify列表里的该notification的显示情况
notification.setLatestEventInfo(this, "下载", "正在下载中", contentIntent);
} else
{
notification = new Notification(R.drawable.title, "下载完毕", System.currentTimeMillis());
// 在此处设置在notify列表里的该notification的显示情况
notification.setLatestEventInfo(this, "下载", "程序下载完毕", contentIntent);
}
//当加上这句默认的震动,声音,LED变量的默认语句时,系统会报错,主要是在没有加上权限。
notification.defaults=Notification.DEFAULT_ALL;
notificationManager.notify(R.menu.main, notification);
}
}
在AndroidManifest.xml中添加"android.permission.VIBRATE"权限即可解决。因为Notification设置了DEFAULT_ALL默认包含了DEFAULT_VIBRATE,而DEFAULT_VIBRATE是要求VIBRATE震动权限的,没有添加该权限的话,当程序有震动请求时自然就会报错退出了。
AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE"/>
//通知时震动效果
m_Notification.defaults = Notification.DEFAULT_VIBRATE;
//通知时屏幕发亮
m_Notification.defaults = Notification.DEFAULT_LIGHTS;
//通知时既震动又屏幕发亮还有默认的声音
m_Notification.defaults = Notification.DEFAULT_ALL;