[Android]后台Service 弹出自定义dialog
2013-08-12 14:29
302 查看
DEMO下载路径:http://download.csdn.net/detail/u011634756/5915483 (免积分哦)
核心代码:
Service里面进行 注册广播和 自定义dialog的制作和弹出@Override public void onCreate() { // 注册一个 receiver 并每分钟弹出一个dialog // Intent.ACTION_TIME_TICK 这个广播每分钟接收一次 IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_TIME_TICK); mReceiver = new TimeTickReceiver(); this.registerReceiver(mReceiver, intentFilter); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub showDialog(); return super.onStartCommand(intent, flags, startId); } // 创建dialog 的地方 private void createDialog() { View view = View.inflate(TestServer.this, R.layout.dialog, null); // 初始Dialog 里面的内容 dialogBuOK = (Button) view.findViewById(R.id.dialog_button_ok); dialogBuOK.setOnClickListener(this); dialogBuCancel = (Button) view.findViewById(R.id.dialog_button_cancel); dialogBuCancel.setOnClickListener(this); dialogContent = (TextView)view.findViewById(R.id.dialog_content); AlertDialog.Builder builder = new AlertDialog.Builder(TestServer.this); builder.setView(view); mDialog = builder.create(); mDialog.getWindow().setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); } // 更新dialog 里面的内容 private String updateDialogContent(){ String content = null ; SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss"); String date=sdf.format(new java.util.Date()); content = getString(R.string.dialog_content) + date; return content; } // 弹出dialog 的地方 public void showDialog() { if (mDialog == null) { createDialog(); } dialogContent.setText(updateDialogContent()); mDialog.dismiss(); mDialog.show(); } @Override public void onDestroy() { // 在服务结束的时候把 注册的广播给结束掉 unregisterReceiver(mReceiver); super.onDestroy(); } // 处理2个button 的点击事件 @Override public void onClick(View v) { switch (v.getId()) { case R.id.dialog_button_ok: //TODO mDialog.dismiss(); break; case R.id.dialog_button_cancel: //TODO mDialog.dismiss(); break; default: break; } }
广播接收器里面 接收到ACTION_TIME_TICK 就去更新一下 Dialog的里面的内容
public class TimeTickReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("TimeTickReceiver", "onReceive...."); Intent mIntent = new Intent(); mIntent.setClass(context, TestServer.class); context.startService(mIntent); } }
以上就是本demo里面的核心代码,剩下的就是 启动服务了
效果图:
注:
1. 本demo中一个好用的action Intent.ACTION_TIME_TICK
这一个广播是由系统发出
如果想要接收到这个广播 必须是通过 动态注册的广播接收器 才能接收到哦
在 Androidmanifest 里面注册的广播是接受不到这个广播的
2. 大家都知道 Dialog必须要基于Activity来实现 如果没有Activity 那么我们就需要这么来实现
Dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
并添加权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
====================关注小鱼===================
新浪微博:http://weibo.com/AwareMissing
腾讯微博:http://t.qq.com/y_haiyang
相关文章推荐
- android在Service,BroadCast onReceiver()中弹出Dialog对话框,即全局性对话框
- Android自定义Dialogの仿ios系统底部菜单(自下而上弹出)
- Android自定义对话框(Dialog)位置,大小(Service中同样适用)
- android中自定义的dialog中的EditText无法弹出输入法解决方案
- android的service中在后台弹出提示框
- Android学习问题:关于AlertDialog中自定义布局带有的EditText无法弹出键盘
- Android中Dialog中自定义EditView导致软键盘无法弹出
- android 自定义dialog弹出和消失动画
- Android Service 弹出系统全局Dialog
- android service 如何弹出dialog
- Android--自定义弹出框-自定义dialog
- android 自定义dialog弹出和消失缩放动画
- Android菜鸟笔记-Service中弹出对话框,即全局性质Dialog不依赖于Activity
- Android自定义dialog中的EditText无法弹出键盘的解决
- Android自定义dialog中的EditText无法弹出键盘的解决
- android中自定义的dialog中的EditText无法弹出输入法解决方案
- Android自定义dialog 自下往上弹出的实例代码
- 关于android在Service中弹出Dialog对话框
- Android自定义dialog从屏幕底部弹出并且充满屏幕宽度
- android service中弹出dialog对话框