一、建立NotificationService
public class NotificationService extends Service {
private static final String TAG = "NotificationService";
private ActivityManager activityManager;
private String packageName;
private boolean isStop = false;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
activityManager = (ActivityManager) this
.getSystemService(Context.ACTIVITY_SERVICE);
packageName = this.getPackageName();
System.out.println("启动服务");
new Thread() {
public void run() {
try {
while (!isStop) {
Thread.sleep(1000);
if (isAppOnForeground()) {
// Log.v(TAG, "前台运行");
} else {
// Log.v(TAG, "后台运行");
showNotification();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
/**
* 程序是否在前台运行
*
* @return
*/
public boolean isAppOnForeground() {
// Returns a list of application processes that are running on the
// device
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
if (appProcesses == null)
return false;
for (RunningAppProcessInfo appProcess : appProcesses) {
// The name of the process that this object is associated with.
if (appProcess.processName.equals(packageName)
&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
}
}
return false;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("终止服务");
isStop = true;
}
// 显示Notification
public void showNotification() {
// 创建一个NotificationManager的引用
NotificationManager notificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
Notification notification = new Notification(R.drawable.ic_launcher,
"qtt", System.currentTimeMillis());
// 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_ONGOING_EVENT;
// 点击后自动清除Notification
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 5000;
// 设置通知的事件消息
CharSequence contentTitle = "qtt"; // 通知栏标题
CharSequence contentText = "推送信息显示,请查看……"; // 通知栏内容
Intent notificationIntent = new Intent(ApplicationActivity.context,
ApplicationActivity.context.getClass());
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(
ApplicationActivity.context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(ApplicationActivity.context,
contentTitle, contentText, contentIntent);
// 把Notification传递给NotificationManager
notificationManager.notify(0, notification);
}
}
其中ApplicationActivity.context是在Activity中赋值的,
二、j接受或者拒绝对话框做为Dialog形式显示,代码如下:
Intent intent = new Intent(getApplicationContext(),
VideoCallDialog.class);
startActivity(intent);
这样就可以实现与QQ一样的呼叫弹处界面了