package com.zhike.sync;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import com.zhike.R;
import com.zhike.activity.Settings;
import com.zhike.util.TimeDiary;
public class SyncService extends Service {
private static final String TAG = "sync";
private static final String FAIL = "0";
private static final String SUCCESS = "1";
private static final String NOT_FOUND = "2";
private static String WEB_URL = "";
private static boolean RUN_FLAG = false;
private static int INTERVAL = 60;
private static boolean NEED_SYNC;
private SharedPreferences sharePreferences;
@Override
public void onCreate() {
super.onCreate();
sharePreferences = PreferenceManager.getDefaultSharedPreferences(this);
INTERVAL = Integer.valueOf(sharePreferences.getString(
"sync_interval_list", "60"));
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, SyncService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,
0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System
.currentTimeMillis() + 60 * 1000, (INTERVAL * 60 * 1000),
pendingIntent);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// 鑾峰彇璁剧疆
NEED_SYNC = sharePreferences.getBoolean("sync_config_checkbox", false);
WEB_URL = sharePreferences.getString("sync_url_edit", "");
if (NEED_SYNC) {
if ("".equals(WEB_URL) || "http://".equals(WEB_URL)) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification nf = new Notification(R.drawable.icon,
getString(R.string.notice_title), System
.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, new Intent(this, Settings.class), 0);
nf.setLatestEventInfo(this, getString(R.string.app_name),
getString(R.string.notice_message), contentIntent);
manager.notify(R.string.notice_title, nf);
return;
}
Thread thread = new Thread(null, doBackgroundThreadProcessing,
"Background");
thread.start();
}
}
private String syncOne(String webUrl, String type, String method,
String body) {
HttpURLConnection con = null;
try {
body = URLEncoder.encode(body, "UTF-8");
URL url = new URL(webUrl + "?type=" + type + "&method=" + method
+ "&body=" + body);
con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000 /* milliseconds */);
con.setConnectTimeout(15000 /* milliseconds */);
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(
con.getInputStream(), "UTF-8"));
String result = reader.readLine();
reader.close();
if (result != null) {
if ("update".equals(method) && SUCCESS.equals(result.trim())) {
return SUCCESS;
}
if ("delete".equals(method)
&& (SUCCESS.equals(result.trim()) || NOT_FOUND
.equals(result.trim()))) {
return SUCCESS;
}
}
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "UnsupportedEncodingException", e);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException", e);
} catch (IOException e) {
Log.e(TAG, "IOException", e);
}
return FAIL;
}
private Runnable doBackgroundThreadProcessing = new Runnable() {
public void run() {
if (RUN_FLAG)
return;
RUN_FLAG = true;
Cursor cursor = getNeedSyncLog();
cursor.moveToFirst();
long id = 0;
long pid = 0;
String type = "";
String method = "";
String body = "";
try {
while (!cursor.isAfterLast()) {
id = cursor.getLong(cursor.getColumnIndex("_id"));
type = cursor.getString(cursor.getColumnIndex("type"));
method = cursor.getString(cursor.getColumnIndex("method"));
pid = cursor.getLong(cursor.getColumnIndex("pid"));
body = getBody(type, method, pid);
if ("".equals(body)) {
cursor.moveToNext();
continue;
}
try {
if (SUCCESS
.equals(syncOne(WEB_URL, type, method, body))) {
updateSyncLogState(id, SUCCESS);
cursor.moveToNext();
} else {
updateSyncLogState(id, FAIL);
break;
}
} catch (Exception e) {
Log.d("SyncService", "自动同步更新出错!");
cursor.moveToNext();
continue;
}
}
cursor.close();
deleteSuccessSyncLog();
} catch (Exception e) {
Log.d("SyncService", "获取自动同步数据出错!");
}
RUN_FLAG = false;
}
};
// 组装消息内容
private String getBody(String type, String method, long pid) {
String body = "";
if ("delete".equals(method)) {
body = "pid=" + pid;
deleteSuccessSyncLog(pid);
} else if ("update".equals(method)) {
Cursor mCursor;
if ("eventtype".equals(type)) {
mCursor = getContentResolver().query(
Uri.withAppendedPath(
TimeDiary.EVENT_TYPE_CONTENT_URI, "/" + pid),
new String[] { "name" }, null, null, null);
if (mCursor != null & mCursor.getCount() > 0) {
mCursor.moveToFirst();
String name = mCursor.getString(mCursor
.getColumnIndex("name"));
body = "pid=" + pid + "&name=" + name;
mCursor.close();
}
} else if ("timeitem".equals(type)) {
mCursor = getContentResolver().query(
Uri.withAppendedPath(TimeDiary.DIARY_ITEM_CONTENT_URI,
"/" + pid),
new String[] { "content", "date", "hour", "minute",
"rate", "event_type" }, null, null, null);
if (mCursor != null && mCursor.getCount() > 0) {
mCursor.moveToFirst();
String content = mCursor.getString(mCursor
.getColumnIndex("content"));
String date = mCursor.getString(mCursor
.getColumnIndex("date"));
int hour = mCursor.getInt(mCursor.getColumnIndex("hour"));
int minute = mCursor.getInt(mCursor
.getColumnIndex("minute"));
int rate = mCursor.getInt(mCursor.getColumnIndex("rate"));
long eventtype = mCursor.getLong(mCursor
.getColumnIndex("event_type"));
body = "pid=" + pid + "&content=" + content + "&date="
+ date + "&hour=" + hour + "&minute=" + minute
+ "&rate=" + rate + "&eventtype=" + eventtype;
mCursor.close();
}
} else if ("introspection".equals(type)) {
mCursor = getContentResolver().query(
Uri.withAppendedPath(
TimeDiary.THOUGHTS_CONTENT_URI, "/"
+ pid),
new String[] { "introspection", "date" }, null, null,
null);
if (mCursor != null & mCursor.getCount() > 0) {
mCursor.moveToFirst();
String introspection = mCursor.getString(mCursor
.getColumnIndex("introspection"));
String date = mCursor.getString(mCursor
.getColumnIndex("date"));
body = "pid=" + pid + "&introspection=" + introspection
+ "&date=" + date;
mCursor.close();
}
}
}
return body;
}
private Cursor getNeedSyncLog() {
Cursor cursor = (Cursor) getContentResolver().query(
TimeDiary.SYNC_LOG_CONTENT_URI,
new String[] { "_id", "type", "method",
没有合适的资源?快使用搜索试试~ 我知道了~
Android个人日程管理(附源码)
共111个文件
class:47个
java:19个
xml:15个
4星 · 超过85%的资源 需积分: 50 551 下载量 139 浏览量
2012-10-04
19:32:30
上传
评论 29
收藏 352KB RAR 举报
温馨提示
个人日程管理的小软件,主要使用了SQLite的各种操作、ContentPrivider的定义和访问、各种复合布局、对屏幕滑动事件的处理、receiver和service的应用。
资源推荐
资源详情
资源评论
收起资源包目录
Android个人日程管理(附源码) (111个子文件)
all-wcprops 166B
resources.ap_ 80KB
TimeDiary.apk 110KB
SyncService.class 10KB
DiaryList.class 9KB
DiaryNew.class 8KB
DiaryProvider.class 8KB
CalendarView.class 5KB
Main.class 5KB
TypeActivity.class 5KB
ThoughtsActivity.class 5KB
DiaryAdapter.class 4KB
TypeAdapter.class 3KB
DiaryNew$1.class 3KB
TimeDiary.class 3KB
Settings$2.class 3KB
SyncService$1.class 2KB
TypeActivity$3.class 2KB
DiaryList$1.class 2KB
Settings$1.class 2KB
DatabaseUtil.class 2KB
Settings.class 2KB
TypeActivity$4.class 2KB
TypeActivity$2.class 2KB
SyncLogAdapter.class 2KB
CalendarView$1.class 2KB
CalendarActivity.class 2KB
TypeActivity$1.class 2KB
Main$1.class 1KB
ThoughtsActivity$1.class 1KB
CalendarView$DateRect.class 1KB
IntentReceiver.class 1KB
R$string.class 1KB
DiaryList$3.class 1KB
DiaryList$2.class 1KB
DiaryList$4.class 996B
TypeActivity$5.class 988B
R$id.class 964B
About.class 908B
DiaryNew$1$1.class 841B
R$drawable.class 836B
TimeDiary$1.class 749B
R.class 620B
R$layout.class 594B
R$color.class 551B
ThoughtsAdapter.class 460B
R$style.class 435B
R$array.class 435B
R$xml.class 362B
R$attr.class 313B
.classpath 280B
Thumbs.db 29KB
Thumbs.db 9KB
Thumbs.db 7KB
Thumbs.db 7KB
classes.dex 58KB
entries 371B
icon_list.gif 2KB
icon_newnote.gif 2KB
icon_type.gif 2KB
handle_dark.gif 839B
SyncService.java 9KB
DiaryList.java 8KB
DiaryProvider.java 8KB
DiaryNew.java 7KB
Main.java 5KB
R.java 5KB
TypeActivity.java 5KB
CalendarView.java 5KB
ThoughtsActivity.java 4KB
Settings.java 4KB
DiaryAdapter.java 3KB
TypeAdapter.java 2KB
DatabaseUtil.java 2KB
TimeDiary.java 2KB
SyncLogAdapter.java 1017B
CalendarActivity.java 774B
IntentReceiver.java 723B
About.java 370B
ThoughtsAdapter.java 214B
background.jpg 36KB
icon.png 7KB
icon.png 5KB
icon.png 5KB
icon.png 5KB
summary.png 4KB
calendar.png 4KB
rate_star_med_on.png 1KB
rate_star_med_half.png 1008B
flower3.png 657B
leaves4.png 615B
rate_star_med_off.png 379B
org.eclipse.core.resources.prefs 141B
.project 845B
default.properties 364B
settings.xml.svn-base 1KB
diary.xml 3KB
AndroidManifest.xml 2KB
diarytype.xml 2KB
strings.xml 2KB
共 111 条
- 1
- 2
master0o0
- 粉丝: 16
- 资源: 59
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页