首先,先把我之前寫的那個更新操作的一個bug修複先,這個bug就是在android4以上,我們已經不能在主線程裡面進行網絡操作的啦,如果進行網絡操作,就會報錯
是以我們就要做一下改動,我們在oncreate方法裡面開啟一個線程,用來檢測更新資訊的
<font color="#333333"><font face="arial"> new thread()
{
public void run()
{
try
{
updateinfoservice updateinfoservice = new updateinfoservice(splashactivity.this);
info = updateinfoservice.getupdateinfo(r.string.serverurl);
}
catch (exception e)
e.printstacktrace();
};
}.start();</font></font>
複制代碼
然後呢,就在原來的isneedupdate方法裡面進行一些修改啦
<font color="#333333"><font face="arial"> private boolean isneedupdate(string version)
{
if(info == null)
toast.maketext(this, "擷取更新資訊異常,請稍後再試", toast.length_short).show();
loadmainui();
return false;
}
string v = info.getversion();
if(v.equals(version))
log.i(tag, "目前版本:" + version);
log.i(tag, "最新版本:" + v);
else
log.i(tag, "需要更新");
return true;
}</font></font>
好啦,到這裡,我們的這個bug就修複的啦。在進入今天的内容之前,先謝謝一位網友啦,是他發現這個問題的,非常感謝!好,我們今天要講的就是設定手機歸屬地的樣式以及顯示位置,先上圖,讓大家看一下我們今天要做的效果
大家可以看到,我們今天要做的就是顯示風格和位置,那個顯示風格就是上面的第二張圖檔,其實這個功能很簡單的,就是記錄下來使用者設定的風格,然後就進行一個背景的設定而已,而第二個要做的就是顯示的位置啦,上面第三張圖就是啦,我們把那個activity做成了透明的了,如果你覺得不好,那麼也可以不做成透明的,我們可以手動那個紫色的方塊,來确定要顯示的位置!設定好這些之後,再把服務開啟,我們就可以看到下面的效果的啦!
好,廢話不多說,我們現在就開始做,我們首先把風格那個對話框給做出來com.xiaobin.security.ui.atoolactivity
package com.xiaobin.security.ui;
import java.io.file;
import android.annotation.suppresslint;
import android.app.activity;
import android.app.alertdialog;
import android.app.progressdialog;
import android.content.context;
import android.content.dialoginterface;
import android.content.intent;
import android.content.sharedpreferences;
import android.content.sharedpreferences.editor;
import android.graphics.color;
import android.os.bundle;
import android.os.environment;
import android.os.handler;
import android.os.message;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.checkbox;
import android.widget.compoundbutton;
import android.widget.compoundbutton.oncheckedchangelistener;
import android.widget.textview;
import android.widget.toast;
import com.xiaobin.security.r;
import com.xiaobin.security.engine.downloadtask;
import com.xiaobin.security.service.addressservice;
public class atoolactivity extends activity implements onclicklistener
{
private static final int error = 0;
private static final int success = 1;
private textview tv_atool_query;
private textview tv_atool_number_service_state;
private checkbox cb_atool_state;
private textview tv_atool_select_bg;
private textview tv_atool_change_location;
private intent serviceintent;
private progressdialog pd;
private sharedpreferences sp;
@suppresslint("handlerleak")
private handler handler = new handler()
public void handlemessage(message msg)
switch(msg.what)
case error :
toast.maketext(atoolactivity.this, "下載下傳資料庫失敗,請檢查網絡!", toast.length_short).show();
break;
case success :
toast.maketext(atoolactivity.this, "資料庫下載下傳成功!", toast.length_short).show();
default :
}
};
@override
protected void oncreate(bundle savedinstancestate)
super.oncreate(savedinstancestate);
setcontentview(r.layout.atool);
sp = getsharedpreferences("config", context.mode_private);
tv_atool_query = (textview) findviewbyid(r.id.tv_atool_query);
tv_atool_query.setonclicklistener(this);
tv_atool_select_bg = (textview) findviewbyid(r.id.tv_atool_select_bg);
tv_atool_select_bg.setonclicklistener(this);
tv_atool_change_location = (textview) findviewbyid(r.id.tv_atool_change_location);
tv_atool_change_location.setonclicklistener(this);
tv_atool_number_service_state = (textview) findviewbyid(r.id.tv_atool_number_service_state);
cb_atool_state = (checkbox) findviewbyid(r.id.cb_atool_state);
serviceintent = new intent(this, addressservice.class);
cb_atool_state.setoncheckedchangelistener(new oncheckedchangelistener()
@override
public void oncheckedchanged(compoundbutton buttonview, boolean ischecked)
if(ischecked)
startservice(serviceintent);
tv_atool_number_service_state.settextcolor(color.black);
tv_atool_number_service_state.settext("歸屬地服務已開啟");
else
stopservice(serviceintent);
tv_atool_number_service_state.settextcolor(color.red);
tv_atool_number_service_state.settext(r.string.number_service_state);
});
}
public void onclick(view v)
switch(v.getid())
case r.id.tv_atool_query :
query();
break;
case r.id.tv_atool_select_bg :
selectstyle();
case r.id.tv_atool_change_location :
intent intent = new intent(this, dragviewactivity.class);
startactivity(intent);
default :
private void query()
if(isdbexist())
intent intent = new intent(this, querynumberactivity.class);
startactivity(intent);
//提示使用者下載下傳資料庫
pd = new progressdialog(this);
pd.setmessage("正在下載下傳資料庫...");
pd.setprogressstyle(progressdialog.style_horizontal);
pd.setcancelable(false);
pd.show();
new thread()
public void run()
string path = getresources().getstring(r.string.serverdb);
file dir = new file(environment.getexternalstoragedirectory(), "/security/db");
if(!dir.exists())
{
dir.mkdirs();
}
string dbpath = environment.getexternalstoragedirectory() + "/security/db/data.db";
try
//這個類,我們在做更新apk的時候已經寫好的啦,現在直接拿過來用就可以啦
downloadtask.getfile(path, dbpath, pd);
pd.dismiss();
catch (exception e)
e.printstacktrace();
message message = new message();
message.what = error;
handler.sendmessage(message);
};
}.start();
private boolean isdbexist()
if(environment.getexternalstoragestate().equals(environment.media_mounted))
file file = new file(environment.getexternalstoragedirectory() + "/security/db/data.db");
if(file.exists())
return true;
return false;
//顯示風格的對話框,我準備了5張不同風格的背景圖檔,根據使用者選擇的風格不一樣,設定不同的背景
private void selectstyle()
alertdialog.builder builder = new alertdialog.builder(this);
builder.settitle("歸屬地顯示風格");
string[] items = new string[] {"半透明", "活力橙", "蘋果綠", "孔雀藍", "金屬灰"};
builder.setsinglechoiceitems(items, 0, new dialoginterface.onclicklistener()
public void onclick(dialoginterface dialog, int which)
editor editor = sp.edit();
editor.putint("background", which);
editor.commit();
builder.setpositivebutton(android.r.string.ok, new dialoginterface.onclicklistener()
builder.create().show();
}
我們寫了一個方法,selectstyle這個方法會把使用者的設定記錄下來,然後我們就在顯示那個歸屬地那裡讀取出來,設定一下背景就可以啦,但我們現在先把那個顯示位置的也做出來先其實這個也很簡單的,我們就用一個ontouch事件,然後記錄下最終的位置就可以的啦com.xiaobin.security.ui.dragviewactivity
import android.view.motionevent;
import android.view.view.ontouchlistener;
import android.view.window;
import android.widget.imageview;
import android.widget.relativelayout;
import android.widget.relativelayout.layoutparams;
public class dragviewactivity extends activity implements ontouchlistener
private imageview iv_drag_location;
//記錄第一次觸摸的坐标
private int startx;
private int starty;
requestwindowfeature(window.feature_no_title);
setcontentview(r.layout.drag_view);
iv_drag_location = (imageview) findviewbyid(r.id.iv_drag_location);
iv_drag_location.setontouchlistener(this);
protected void onresume()
super.onresume();
//加載上次移動的效果
int x = sp.getint("lastx", 0);
int y = sp.getint("lasty", 0);
/*iv_drag_location.layout(iv_drag_location.getleft() + x, iv_drag_location.gettop() + y,
iv_drag_location.getright() + x, iv_drag_location.getbottom() + y);
iv_drag_location.invalidate();*/
relativelayout.layoutparams params = (layoutparams) iv_drag_location.getlayoutparams();
params.leftmargin = x;
params.topmargin = y;
iv_drag_location.setlayoutparams(params);
public boolean ontouch(view v, motionevent event)
case r.id.iv_drag_location :
switch(event.getaction())
case motionevent.action_down :
startx = (int) event.getrawx();
starty = (int) event.getrawy();
break;
case motionevent.action_move :
int x = (int) event.getrawx();
int y = (int) event.getrawy();
//算出移動距離
int dx = x - startx;
int dy = y - starty;
int l = iv_drag_location.getleft();
int r = iv_drag_location.getright();
int t = iv_drag_location.gettop();
int b = iv_drag_location.getbottom();
//設定新的布局位置
iv_drag_location.layout(l + dx, t + dy, r + dx, b + dy);
//重新擷取位置
case motionevent.action_up :
int lastx = iv_drag_location.getleft();
int lasty = iv_drag_location.gettop();
editor editor = sp.edit();
editor.putint("lastx", lastx);
editor.putint("lasty", lasty);
editor.commit();
default :
return true;
好啦,寫完這兩個,我們就可以回到我們之前顯示歸屬地的那個service裡面寫邏輯啦com.xiaobin.security.service.addressservice
package com.xiaobin.security.service;
import android.app.service;
import android.graphics.pixelformat;
import android.os.ibinder;
import android.telephony.phonestatelistener;
import android.telephony.telephonymanager;
import android.view.gravity;
import android.view.windowmanager;
import android.widget.linearlayout;
import com.xiaobin.security.engine.numberaddressservice;
public class addressservice extends service
private telephonymanager telephonymanager;
private myphonelistener listener;
private windowmanager windowmanager;
private view view;
public ibinder onbind(intent intent)
return null;
public void oncreate()
super.oncreate();
windowmanager = (windowmanager) getsystemservice(context.window_service);
listener = new myphonelistener();
telephonymanager = (telephonymanager) getsystemservice(context.telephony_service);
telephonymanager.listen(listener, phonestatelistener.listen_call_state);
public void ondestroy()
super.ondestroy();
//停止監聽
telephonymanager.listen(listener, phonestatelistener.listen_none);
//顯示歸屬地的窗體
private void showlocation(string address)
windowmanager.layoutparams params = new windowmanager.layoutparams();
params.width = windowmanager.layoutparams.wrap_content;
params.height = windowmanager.layoutparams.wrap_content;
params.flags = windowmanager.layoutparams.flag_not_focusable //無法擷取焦點
| windowmanager.layoutparams.flag_not_touchable //無法點選
| windowmanager.layoutparams.flag_keep_screen_on;//保持螢幕亮
params.format = pixelformat.translucent;//設定成半透明的
params.type = windowmanager.layoutparams.type_toast;
params.settitle("toast");
//主要是确定坐标系是從左上角開始的,不然呆會設定位置的時候有些麻煩
params.gravity = gravity.left | gravity.top;
params.x = sp.getint("lastx", 0);
params.y = sp.getint("lasty", 0);
view = view.inflate(getapplicationcontext(), r.layout.show_location, null);
linearlayout ll = (linearlayout) view.findviewbyid(r.id.ll_location);
int type = sp.getint("background", 0);
switch(type)
case 0 :
ll.setbackgroundresource(r.drawable.call_locate_white);
case 1 :
ll.setbackgroundresource(r.drawable.call_locate_orange);
case 2 :
ll.setbackgroundresource(r.drawable.call_locate_green);
case 3 :
ll.setbackgroundresource(r.drawable.call_locate_blue);
case 4 :
ll.setbackgroundresource(r.drawable.call_locate_gray);
textview tv = (textview) view.findviewbyid(r.id.tv_show_location);
tv.settext("歸屬地: " + address);
windowmanager.addview(view, params);
//========================================================================
private class myphonelistener extends phonestatelistener
@override
public void oncallstatechanged(int state, string incomingnumber)
super.oncallstatechanged(state, incomingnumber);
switch(state)
case telephonymanager.call_state_idle : //空閑狀态
if(view != null)
windowmanager.removeview(view);//移除顯示歸屬地的那個view
view = null;
case telephonymanager.call_state_offhook : //接通電話
case telephonymanager.call_state_ringing : //鈴響狀态
string address = numberaddressservice.getaddress(incomingnumber);
showlocation(address);
好啦,到現在為止,我們的顯示風格還有顯示位置就已經全部完成的啦!下次我們就會講進階工具裡面的黑名單功能的啦!
kb, 下載下傳次數: 81)