Android - 终于写出了自己app需要使用的服务
来源:互联网 发布:shadowsock服务器端口 编辑:程序博客网 时间:2024/04/27 04:34
需求:
(搭车请求信息)-应用在后台运行,凡设置为允许接收搭车请求者,并且距离在5KM之内的用户,均会收到提示“**用户在距离你**米距离处靠近**地址,想到**目的地去,正在等待搭车,希望得到你的帮助!”,提示以最前面窗口式显示,发出提示声音“请求搭车信息”,显示倒计时10秒后,窗口自动关闭。
设计:首先,这个服务得一直在后台运行,监听Server发送过来的请求信息,然后发送给Activity显示。
其次,显示一个对话框,当然对话框不能阻碍用户当前的Activity,这里把dialog设置成TYPE_SYSTEM_ALERT。
最后,显示倒计时10秒。
初步实现的Code(不带跟服务的交互):
需要加使用权限: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
/** * ShareCarBGService is a service run in background and * deliver msgs from server to user. * Attention: build thread for self. * @author xinyan * @date 2011-10-23 */public class ShareCarBGService extends Service {public static final String TAG = "ReserveActivity";public static final int MSG_DIALOG_TIME_COUNT = 0;public static final int MSG_SHOW_REQUEST_DIALOG = 1;private static AlertDialog mDialog;private final String mDialogTitle = "收到搭车请求";String strRequest = "**用户在距离你**米距离处靠近**地址,想到**目的地去,正在等待搭车,希望得到你的帮助!";private Looper mServiceLooper;private ServiceHandler mServiceHandler;private Runnable mDialogRunnable;private int mTimeCount = 10;// Handler that receives messages from the threadprivate final class ServiceHandler extends Handler {public ServiceHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MSG_DIALOG_TIME_COUNT:mServiceHandler.postDelayed(mDialogRunnable, 1000);break;case MSG_SHOW_REQUEST_DIALOG:showNewRequestDialog(strRequest);break;default:break;}}}@Overridepublic void onCreate() {super.onCreate();// Start up the thread running the service. Note that we create a// separate thread because the service normally runs in the process's// main thread, which we don't want to block. We also make it// background priority so CPU-intensive work will not disrupt our UI.HandlerThread thread = new HandlerThread("ServiceStartArguments",Process.THREAD_PRIORITY_BACKGROUND);thread.start();// Get theHandlerThread's Looper and use it for our HandlermServiceLooper = thread.getLooper();mServiceHandler = new ServiceHandler(mServiceLooper);Log.v(TAG, "service started.");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// For each start request, send a message to start a job and deliver the// start ID so we know which request we're stopping when we finish the// jobMessage msg = mServiceHandler.obtainMessage();msg.arg1 = startId;msg.what = MSG_SHOW_REQUEST_DIALOG;mServiceHandler.sendMessageDelayed(msg, 5000);mDialogRunnable = new Runnable() {@Overridepublic void run() {if (mTimeCount > 0) {mDialog.setTitle(mDialogTitle + "(" + mTimeCount + ")");mTimeCount--;mServiceHandler.postDelayed(mDialogRunnable, 1000);} else { // 10s later, dismiss the request msg dialog.mDialog.dismiss();}}};Log.v(TAG, "onStartCommand");// If we get killed, after returning from here, restartreturn START_STICKY;}@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {super.onDestroy();Log.v(TAG, "service destroyed.");}/** * show a msg dialog when a new pickride request received. * @param request the content of request */public void showNewRequestDialog(String request) {// i don't know whether a static dialog can use as this.if (null == mDialog) {mDialog = new AlertDialog.Builder(this).create();mDialog.setTitle(mDialogTitle);mDialog.setMessage(request);// This the point, this dialog won't block the user's current// activity// via this attrmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});mDialog.setButton(DialogInterface.BUTTON_POSITIVE, "查看",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// Bundle bundle = new Bundle();Intent intent = new Intent();intent.setClass(ShareCarBGService.this,ReserveRequestListTabWidgetActivity.class);startActivity(intent);}});} else {mDialog.setMessage(request);}mDialog.show();mServiceHandler.sendEmptyMessage(MSG_DIALOG_TIME_COUNT);}}
- Android - 终于写出了自己app需要使用的服务
- 终于第一次自己写出了递归
- C#初学者—— 终于写出了自己第一个比较满意的程序
- 终于写出一个MFC的程序了
- 终于知道自己需要什么了
- 终于用上了自己的BLOG了!
- 终于了又自己的博客了
- 终于开了自己的博客了
- Linux下终于写出了HelloWorld了
- 终于拥有自己的blog了!
- 终于有自己的Blog了
- 终于有自己的blog了
- 终于有了自己的Blog了
- 终于有自己的Blog了!
- 终于有了自己的Blog了
- 终于有了自己的blog了
- 终于开始了一个自己的blog~~
- 终于有我自己的blog了
- SQL Server死锁总结
- “个人Vs.企业谈薪心理战”调查报告
- Linux下调用system()函数导致的问题
- 从一道题谈C++中构造函数调用构造函数
- 操作符重载
- Android - 终于写出了自己app需要使用的服务
- SQL Server 2008中SQL应用之-“死锁(Deadlocking)
- java编程开始力不从心
- rfid1-stc11f32x
- 大干90天驱动入门
- C++沉思录读书笔记(1-3章)
- C++中 十进制,八进制,十六进制间的相互转换输出
- 排序算法总结
- RACI 责任分配矩阵