Written In The Font
花了一個天一夜,搞了這個小東西-安卓手機令牌.其實,我這邊分享,看網上東西也沒多少.這就分享了.因為這個會給公司用.有些東西就不方便透露了.希望大家體諒.還有如果需要找我溝通,加群吧!在頁面中會有三處出現.
Android手機令牌
界面
java 與 C#服務端交互
倒計時和順計時技術
口令生成
Content
android 也不難學,承認花了時間1/2 花在界面上.界面需要經驗的積累.和大哥有說有笑搞著界面,我們浩二有木有.要吐槽的可以吐槽,要啥的說啥.
復制代碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/my_bg"
android:gravity="center_horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/activity_contact_li_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:layout_marginTop="10dp"
android:gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="***"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="40sp" />
</LinearLayout>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="14dp"
android:text="聯系電話:" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView5"
android:layout_alignLeft="@+id/textView5"
android:text="***有限公司 " />
<RelativeLayout
android:id="@+id/activity_contact_li_editphone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btnOperatorRemark"
android:layout_alignLeft="@+id/btnTime"
android:layout_marginBottom="19dp"
android:background="@drawable/table_below_bg"
android:focusable="true"
android:gravity="center_vertical" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/table_below_bg"
android:focusable="true"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10.0dip"
android:duplicateParentState="true"
android:text=" 序列號:"
android:textColor="@color/listitem_gray"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dip"
android:layout_marginRight="10.0dip"
android:layout_weight="1"
android:duplicateParentState="true"
android:textColor="@color/author_text"
android:textSize="14sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/activity_contact_li_mobilephone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/activity_contact_li_editphone"
android:layout_alignLeft="@+id/activity_contact_li_editphone"
android:background="@drawable/table_above_bg"
android:focusable="true"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10.0dip"
android:duplicateParentState="true"
android:text="服務器時間:"
android:textColor="@color/listitem_gray"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dip"
android:layout_marginRight="10.0dip"
android:layout_weight="1"
android:duplicateParentState="true"
android:text=""
android:textColor="@color/author_text"
android:textSize="14sp" />
</LinearLayout>
<Button
android:id="@+id/btnOperatorRemark"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignRight="@+id/activity_contact_li_editphone"
android:layout_alignTop="@+id/btnTime"
android:background="@drawable/login_btn_selector"
android:text="操作說明" />
<Button
android:id="@+id/btnTime"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_above="@+id/textView1"
android:layout_alignLeft="@+id/textView1"
android:layout_marginBottom="24dp"
android:background="@drawable/login_btn_selector"
android:text="時間校准"
android:textSize="18sp" />
<LinearLayout
android:id="@+id/activity_contact_li_mobilephone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_below="@+id/activity_contact_li_title"
android:background="@drawable/table_mid_bg"
android:focusable="true"
android:gravity="center_vertical" >
<TextView
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="60dp"
android:duplicateParentState="true"
android:gravity="center"
android:text="123456"
android:textColor="@color/author_text"
android:textScaleX="1.2"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/activity_contact_li_times"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/activity_contact_li_mobilephone"
android:layout_below="@+id/activity_contact_li_mobilephone"
android:layout_marginTop="17dp"
android:focusable="true"
android:gravity="center" >
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text=">> 口令 "
android:textColor="@color/listitem_blue" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView8"
android:text="22"
android:textColor="@color/red" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=" 秒後更新"
android:textColor="@color/listitem_blue" />
</LinearLayout>
</RelativeLayout>
復制代碼
java 與 C#服務端交互
這種東西 一抓一大把,我這邊簡單就用了下ksoap2-android-assembly-2.4-jar-with-dependencies.jar,發現交互很簡單,url call下然後 調用方法返回值.所以這邊代碼:
復制代碼
private String getDateTime() throws Exception {
// UIHelper.ToastMessage(MainActivity.this, "進去");
SoapObject rpc = new SoapObject(NAMESPACE, GET_TIME_METHOD_NAME);
AndroidHttpTransport ht = new AndroidHttpTransport(URL);
ht.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
try {
ht.call(GET_TIME_SOAP_ACTION, envelope);
} catch (Exception e) {
// 校准時間網絡錯誤
UIHelper.ToastMessage(MainActivity.this, "未連接網絡或網絡異常");
}
SoapObject result = (SoapObject) envelope.bodyIn;
SoapPrimitive detail = (SoapPrimitive) result
.getProperty("GetDateTimeResult");
System.out.println("result" + result);
System.out.println("detail" + detail);
// UIHelper.ToastMessage(MainActivity.this, detail.toString());
return detail.toString();
}
復制代碼
倒計時和順計時技術
android端倒計時:
復制代碼
// 倒計時
Handler handler = new Handler() {
public void handleMessage(Message msg) { // handle message
switch (msg.what) {
case 1:
recLen--;
if (recLen < 10) {
txtTextTimeBack.setText("0" + recLen);
}else{
txtTextTimeBack.setText("" + recLen);
}if (recLen > 0) {
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 1000); // send message
} else {
// 重新獲取序列號
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 1000); // send message
String dateTime;
try {
dateTime = DateFormate.getDateTime();
txtDateTime.setText(dateTime);
} catch (Exception e) {
UIHelper.ToastMessage(MainActivity.this, "未知錯誤");
e.printStackTrace();
}
turnTimeBack();
try {
showTheNumber();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
super.handleMessage(msg);
}
};
復制代碼
順時采用上面倒計時的方法裡面,加起來合為60
復制代碼
String ss = DateFormate.getStringFromDate(DateFormate.getDateFormString(txtDateTime.getText().toString()));
if (recLen > 50)
{
ss += "0" + (60-recLen);
}
else {
ss += "" + (60-recLen);
}
txtDateTime.setText(ss);
復制代碼
口令生成
這是核心關鍵.所以我這邊生成的算法就不方便透露了.像溝通的話,加我的群討論.口令可以根據 序列號 時間 特定字符串生成.這邊我提供下 有關算法,像MD5...等都是常用的
復制代碼
String key = String.valueOf((dateTimeLong ^ serialNumberLong));
String result = null;
MessageDigest mad5 =null;
try
{
mad5 = MessageDigest.getInstance("MD5");
}
catch (Exception e)
{
e.printStackTrace();
}