這裡運用到的andorid知識模塊主要有Notification和Service,和一個android-async-http-master開源框架
android項目中,有時會有這樣一種需求:客戶每隔一段時間,就像服務器發送一個請求,以獲取某些重要的、實時更新的消息。比如天氣預報。
如何讓應用實現在後台一直處於運行狀態,並且每個一段時間就向服務器發一個請求?android裡的四大組件之一:服務,就為我們提供了這種功能。
因此,我們可以嘗試在服務裡邊定義一個線程,只要服務不停止,線程就一直在運行,讓它每隔一段時間,就向服務器發送一個請求。
我們來看一下核心代碼
1.在Activity中,我們可以通過startService()來啟動服務
[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
public void open(View view) {
Intent intent = new Intent(this, PushSmsService.class);
// 啟動服務
startService(intent);
}
2.這裡我們需要自定義一個服務類,去繼承android的Service類
[java]
/**
*
* 短信推送服務類,在後台長期運行,每個一段時間就向服務器發送一次請求
*
* @author jerry
*
*/
public class PushSmsService extends Service {
private MyThread myThread;
private NotificationManager manager;
private Notification notification;
private PendingIntent pi;
private AsyncHttpClient client;
private boolean flag = true;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
System.out.println("oncreate()");
this.client = new AsyncHttpClient();
this.myThread = new MyThread();
this.myThread.start();
super.onCreate();
}
@Override
public void onDestroy() {
this.flag = false;
super.onDestroy();
}
private void notification(String content, String number, String date) {
// 獲取系統的通知管理器
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.ic_menu_compose, content,
System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_ALL; // 使用默認設置,比如鈴聲、震動、閃燈
notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點擊消息後,消息自動在通知欄自動消失
notification.flags |= Notification.FLAG_NO_CLEAR;// 點擊通知欄的刪除,消息不會依然不會被刪除
Intent intent = new Intent(getApplicationContext(),
ContentActivity.class);
intent.putExtra("content", content);
intent.putExtra("number", number);
intent.putExtra("date", date);
pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), number
+ "發來短信", content, pi);
// 將消息推送到狀態欄
manager.notify(0, notification);
}
private class MyThread extends Thread {
@Override
public void run() {
String url = "http://110.65.99.66:8080/jerry/PushSmsServlet";
while (flag) {
System.out.println("發送請求");
try {
// 每個10秒向服務器發送一次請求
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 采用get方式向服務器發送請求
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
try {
JSONObject result = new JSONObject(new String(
responseBody, "utf-8"));
int state = result.getInt("state");
// 假設偶數為未讀消息
if (state % 2 == 0) {
String content = result.getString("content");
String date = result.getString("date");
String number = result.getString("number");
notification(content, number, date);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
Toast.makeText(getApplicationContext(), "數據請求失敗", 0)
.show();
}
});
}
}
}
}
注意我們要在清單文件中注冊Service
[plain]
<service android:name=".PushSmsService"></service>
由於通知信息使用到了手機的震動功能和網絡訪問,所以需要配置權限
[plain]
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
我們再定義一個Activity,用於用戶點擊下拉通知欄裡的某條消息後,跳轉到詳細的消息界面
[java]
public class ContentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
Intent intent = getIntent();
TextView tv_content = (TextView) this.findViewById(R.id.tv_content);
TextView tv_number = (TextView) this.findViewById(R.id.tv_number);
TextView tv_date = (TextView) this.findViewById(R.id.tv_date);
if (intent != null) {
String content = intent.getStringExtra("content");
String number = intent.getStringExtra("number");
String date = intent.getStringExtra("date");
tv_content.setText("內容:" + content);
tv_number.setText("號碼:" + number);
tv_date.setText("日期:" + date);
}
}
}
同樣,我們需要在清單文件中注冊新的Activity
[plain]
<activity android:name=".ContentActivity"></activity>
注意到上邊的代碼是需要服務器提供支持的,我們去新建一個簡單的服務器,裡邊添加一個Servlet和一個實體類就可以了
[java]
public class PushSmsServlet extends HttpServlet {
private static int index = 1;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(index);
// List<Sms> smsList = new ArrayList<Sms>();
Sms sms = new Sms(index,
"我愛你我愛你我愛你我愛你我愛你我愛你我愛你我愛你我愛你我愛你我愛你我愛你" + index, "2013-03-03",
"13522224444");
index++;
// smsList.add(sms);
// sms = new Sms(0, "我真的愛你", "2013-04-04", "13522224444");
// smsList.add(sms);
// sms = new Sms(1, "我真的真的愛你", "2013-05-05", "13522224444");
// smsList.add(sms);
response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
Gson gson = new Gson();
// 將Sms類的數據轉換為json數據格式
String json = gson.toJson(sms);
out.write(json);
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
再定義封裝用於存放消息的類
[java]
public class Sms {
private int state;
private String content;
private String date;
private String number;
public Sms(int state, String content, String date, String number) {
super();
this.state = state;
this.content = content;
this.date = date;
this.number = number;
}
}
這裡只貼出核心代碼