仿QQ呼叫弹出显示

一、建立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一样的呼叫弹处界面了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ada

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值