PollingServiceDemo.java
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2017. wugian
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.xgimi.gimicinema.poll;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
/**
* PollingService Demo do something in PollingThread
*/
public class PollingServiceDemo extends Service {
//name of this service action
public static final String ACTION = "com.qnbar.service.PollingServiceDemo";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.d("polling-demo", "onCreate");
// initNotifiManager();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("polling-demo", "onStartCommand");
new PollingThread().start();
return super.onStartCommand(intent, flags, startId);
}
// @Override
// public void onStart(Intent intent, int startId) {
// new PollingThread().start();
// }
// //初始化通知栏配置
// private void initNotifiManager() {
// mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// int icon = R.drawable.ic_launcher;
// mNotification = new Notification();
// mNotification.icon = icon;
// mNotification.tickerText = "New Message";
// mNotification.defaults |= Notification.DEFAULT_SOUND;
// mNotification.flags = Notification.FLAG_AUTO_CANCEL;
// }
// //弹出Notification
// private void showNotification() {
// mNotification.when = System.currentTimeMillis();
// //Navigator to the new activity when click the notification title
// Intent i = new Intent(this, MessageActivity.class);
// PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,
// Intent.FLAG_ACTIVITY_NEW_TASK);
// mNotification.setLatestEventInfo(this,
// getResources().getString(R.string.app_name), "You have new message!", pendingIntent);
// mManager.notify(0, mNotification);
// }
/**
* Polling thread
* 模拟向Server轮询的异步线程
*
* @Author Ryan
* @Create 2013-7-13 上午10:18:34
*/
int count = 0;
class PollingThread extends Thread {
@Override
public void run() {
Log.d("polling-demo", "polling run");
// System.out.println("Polling...");
count++;
//当计数能被5整除时弹出通知
// if (count % 5 == 0) {
//// showNotification();
// System.out.println("New message!");
// }
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("polling-demo", "onDestroy");
System.out.println("Service:onDestroy");
}
}