編輯:關於Android編程
package com.example.ex_example; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Handler; import android.os.Message; import android.util.Log; import com.unionpay.UPPayAssistEx; import com.unionpay.uppay.PayActivity; public class UnionPay implements Runnable{ // public static final int PLUGIN_VALID = 0; // public static final int PLUGIN_NOT_INSTALLED = -1; // public static final int PLUGIN_NEED_UPGRADE = 2; /***************************************************************** * mMode參數解釋: "00" - 啟動銀聯正式環境 "01" - 連接銀聯測試環境 測試用 卡號:6226440123456785 * 密碼:111101 *****************************************************************/ private final static String mMode = "01"; private static final String TN_URL_01 = "http://202.101.25.178:8080/sim/gettn"; private static ProgressDialog mLoadingDialog = null; private static Activity activity; private static UnionPay unionPay = new UnionPay(); public static void unionpay(Activity activity) { UnionPay.activity = activity; mLoadingDialog = ProgressDialog.show(activity, // context "", // title "正在努力的獲取tn中,請稍候...", // message true); // 進度是否是不確定的,這只和創建進度條有關 /************************************************* * 步驟1:從網絡開始,獲取交易流水號即TN ************************************************/ new Thread(unionPay).start(); // //支付金額 訂單金額 // String orderAmount = "1533"; // //千引商戶號 // String merchantNO = "805920100000069"; // //商戶訂單號 // String orderNO = "467934311"; // //通知Url // String backUrl = "http://www.baidu.com"; // //簽名密鑰 // String key = "86Aab696Fed90bc691a7AfE0b6a269Ac"; // //簽名字段 // String signcont = // merchantNO+"&"+orderNO+"&"+orderAmount+"&"+backUrl+"&"+key; // // Log.e("", ""+signcont); // // String sign = MD5.digest(signcont); // // String sign = MD5Utils.generate(signcont); // Log.e("", "sign = "+sign.length()); // String url = "http://mpay.sinoqy.com:6283/qyapi/trans/getTn" // + "?orderAmount="+orderAmount // + "&merchantNO="+merchantNO // + "&orderNO="+ orderNO // + "&backUrl="+backUrl // + "&Sign="+ sign; // // Log.e("", ""+url); // // //獲取TN // HTTPUtils.get(this, url , new VolleyListener() { // // @Override // public void onErrorResponse(VolleyError arg0) { // Log.e("", "VolleyError= "+arg0); // } // // @Override // public void onResponse(String arg0) { // Log.e("", "onResponse= "+arg0); // } // }); // } // 銀聯 private static Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { if (mLoadingDialog.isShowing()) { mLoadingDialog.dismiss(); } String tn = ""; if (msg.obj == null || ((String) msg.obj).length() == 0) { AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("錯誤提示"); builder.setMessage("網絡連接失敗,請重試!"); builder.setNegativeButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } else { tn = (String) msg.obj; /************************************************* * 步驟2:通過銀聯工具類啟動支付插件 ************************************************/ doStartUnionPayPlugin(activity, tn, mMode); } } }; // 銀聯 public static void doStartUnionPayPlugin(Activity activity, String tn, String mode) { Log.e("", "handleMessage"); // TODO // orderInfo ——訂單信息為交易流水號,即TN。 // mode —— 銀聯後台環境標識,“00”將在銀聯正式環境發起交易, “01”將在 // 銀聯測試環境發起交易 UPPayAssistEx.startPayByJAR(activity, PayActivity.class, null, null, tn, mode); } @Override public void run() { Log.e("", "run"); String tn = null; InputStream is; try { String url = TN_URL_01; URL myURL = new URL(url); URLConnection ucon = myURL.openConnection(); ucon.setConnectTimeout(120000); is = ucon.getInputStream(); int i = -1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((i = is.read()) != -1) { baos.write(i); } tn = baos.toString(); is.close(); baos.close(); } catch (Exception e) { e.printStackTrace(); } Message msg = mHandler.obtainMessage(); msg.obj = tn; mHandler.sendMessage(msg); } }
package com.example.ex_example; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){ //調用 UnionPay.unionpay(this); } }
關於startService的基本使用概述及其生命周期可參見《Android中startService基本使用方法概述》。本文通過播放背景音樂的簡單示例,演示startS
前段時間一直在看有關Binder的知識,最近剛好有時間,總結一些一些知識,希望和大家交流學習。說起Binder我相信大家學android開發的多多少少都對binder有些
本文實例講述了Android控件之ImageView用法。分享給大家供大家參考。具體如下:ImageView控件是一個圖片控件,負責顯示圖片。以下模擬手機圖片查看器目錄結
終於拋棄了卡頓惡心的Eclipse,在使用一個月Android Studio 之後,對Android Studio 的設計實在是非常敬佩,雖然現在Android