一款彩票app的製作運營詳解

(注:本文只介紹代碼實現重點部分。)

app下載地址

前幾日去買彩票,在選號碼時很是苦惱,分析數據吧,咱不會,蒙吧,肯定沒戲。想了一想,爲什麼不做一個生成彩票號碼的app呢?好,說幹就幹。

-----------------------------------------

安卓開發自己已經有了一定的基礎。屢一下思路,也就是界面+簡單算法(生成對應的隨機數序列就可以了)。先給app起個響亮的名字吧,恩。。就叫“小石彩票助手”。

第一大步:開發

在eclipse中新建工程Lottery(彩票)

接着設計下主界面佈局


既然是彩票助手,所以體彩和福彩所有彩種都得支持。(查閱對應官網,找到每種彩票玩法,開獎號是由幾個數字組成等等。)

爲什麼有個”打賞小石“按鈕呢?這個你懂得,不多做解釋。

我們在layout文件夾下建立對應的佈局文件。這裏我採用listview來顯示這些按鈕。而這個listview其實只有一個item。因爲爲了適應不同的手機屏幕,做成只有一個item的listview可以直接對按鈕進行滑動操作。

主界面佈局文件

<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/background"
    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" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
         >
    </ListView>


</RelativeLayout>

接着在MainActivity中對listview進行適配器綁定等操作。

// 獲取listview控件
		ListView lv1 = (ListView) findViewById(R.id.listView1);
		// 綁定適配器
		lv1.setAdapter(new MyAdapter(this));

/**
	 * ListView的適配器實現
	 * 
	 * @author MR.Stone
	 * 
	 */
	class MyAdapter extends BaseAdapter {
		LayoutInflater inflater;
		Context context;

		public MyAdapter(Context context) {
			this.context = context;
		}

		@Override
		public int getCount() {
			// 爲了適應不同機型,纔將開始液麪作爲只有一個item的listview
			return 1;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			inflater = LayoutInflater.from(context);
			// 加載佈局文件
			convertView = inflater.inflate(R.layout.listitem, null);
			init(convertView);
			return convertView;
		}

	}

listitem.xml文件內容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/lanmubeijing"
        android:text="體彩" 
        android:textSize="22dp"
        android:layout_gravity="center_horizontal"/>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageButton
                android:id="@+id/bt_dlt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/daletou" />

            <ImageButton
                android:id="@+id/bt_qxc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/qixingcai" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageButton
                android:id="@+id/bt_pls"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/pailiesan" />

            <ImageButton
                android:id="@+id/bt_plw"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/pailiewu" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>
    </TableLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/lanmubeijing"
        android:text="福彩" 
        android:textSize="22dp"
        android:layout_gravity="center_horizontal"/>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageButton
                android:id="@+id/bt_ssq"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shuangseqiu" 
                android:layout_weight="1"/>

            <ImageButton
                android:id="@+id/bt_qlt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/qilecai" 
                android:layout_weight="1"/>

            <ImageButton
                android:id="@+id/bt_3d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/sandi" 
                android:layout_weight="1"/>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

        </TableRow>

        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>

        <TableRow
            android:id="@+id/tableRow7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/bt_ds"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/lanmubeijing"
        android:text=">>打賞小石" 
        android:textSize="22dp"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

接着創建OpenActivity繼承activity。這是我們的搖獎界面。


在主界面點擊按鈕跳轉至搖獎界面用到的代碼

/**
	 * 按鈕點擊相應事件
	 */
	@Override
	public void onClick(View v) {
		String kind = "";
		switch (v.getId()) {
		case R.id.bt_ds:
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, AwardActivity.class);//打賞小石界面
			startActivity(intent);
			return;
		case R.id.bt_dlt:
			kind = "大樂透";
			break;
		case R.id.bt_qxc:
			kind = "七星彩";
			break;
		case R.id.bt_pls:
			kind = "排列三";
			break;
		case R.id.bt_plw:
			kind = "排列五";
			break;
		case R.id.bt_ssq:
			kind = "雙色球";
			break;
		case R.id.bt_qlt:
			kind = "七樂彩";
			break;
		case R.id.bt_3d:
			kind = "3D";
			break;
		}
		Intent intent = new Intent();
		intent.putExtra("kind", kind);
		intent.setClass(MainActivity.this, OpenActivity.class);
		startActivity(intent);
	}

這個界面要實現的功能是通過搖一搖手機,生成對應的購彩號碼。這時我們新建一個工具類Generate

package com.utils;

import java.util.LinkedList;
import java.util.Random;

import com.xiaoshi.lottery.R;

public class Generate {
	//根據彩票種類生成號碼
	public static String getNum(String kind) {

		if (kind.equals("大樂透")) {
			//getResult生()成1-35中的5個數字
			return getResult(35, 5) + "+ " + getResult(12, 2);
		}
		if (kind.equals("七星彩")) {
			Random rand = new Random();
			String s = rand.nextInt(10) + " " + rand.nextInt(10) + " "
					+ rand.nextInt(10) + " " + rand.nextInt(10) + " "
					+ rand.nextInt(10) + " " + rand.nextInt(10) + " "
					+ rand.nextInt(10);
			return s;

		}
		if (kind.equals("排列三")) {
			Random rand = new Random();
			String s = rand.nextInt(10) + " " + rand.nextInt(10) + " "
					+ rand.nextInt(10);
			return s;

		}
		if (kind.equals("排列五")) {
			Random rand = new Random();
			String s = rand.nextInt(10) + " " + rand.nextInt(10) + " "
					+ rand.nextInt(10) + " " + rand.nextInt(10) + " "
					+ rand.nextInt(10) ;
			return s;
		}
		if (kind.equals("雙色球")) {
			return getResult(33, 6) + "+ " + getResult(16, 1);
		}
		if (kind.equals("七樂彩")) {
			return getResult(30, 7);
		}
		if (kind.equals("3D")) {
			Random rand = new Random();
			String s = rand.nextInt(10) + " " + rand.nextInt(10) + " "
					+ rand.nextInt(10);
			return s;
		}
		return "發生錯誤";
	}

	public static int getKindId(String kind) {

		if (kind.equals("大樂透")) {

			return 1;
		}
		if (kind.equals("七星彩")) {
			
			return 1;

		}
		if (kind.equals("排列三")) {
			return 1;

		}
		if (kind.equals("排列五")) {
			return 1;
		}
		if (kind.equals("雙色球")) {
			return 0;
		}
		if (kind.equals("七樂彩")) {
			return 0;
		}
		if (kind.equals("3D")) {
			return 0;
		}
		return 0;
	}
	
	static String getResult(int total, int nums) {
		// 用list來裝載開獎號碼
		LinkedList<Integer> listNum = new LinkedList<Integer>();
		for (int i = 0; i < total; i++) {
			listNum.add(i + 1);
		}
		// 用來生成隨機數
		Random rand = new Random();
		String result = "";
		int k;
		for (int i = 0; i < nums; i++) {
			k = rand.nextInt(total);
			result += (listNum.get(k) > 9 ? (listNum.get(k) + "")
					: ("0" + listNum.get(k))) + " ";
			listNum.remove(k);
			total--;
		}

		return result;
	}
}

然後在OpenActivity實現搖一搖功能後調用getNum(kind)方法。

搖一搖實現功能

private void rock() {
		// 1獲得硬件信息
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
		activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

		// 2 判斷當前手機是否帶加速度感應器,如果不帶,直接結束,不啓動服務
		List<Sensor> sensors = mSensorManager
				.getSensorList(Sensor.TYPE_ACCELEROMETER);
		if (sensors != null)
			if (sensors.size() == 0)
				return;

		// 3生成感應偵聽事件
		sensorelistener = new SensorEventListener() {
			@Override
			public void onAccuracyChanged(Sensor sensor, int accuracy) {
				// TODO Auto-generated method stub

			}

			// 感應器發生改變
			@Override
			public void onSensorChanged(SensorEvent event) {
				// TODO Auto-generated method stub
				int sensorType = event.sensor.getType();

				// 讀取搖一搖敏感值
				int shakeSenseValue = 18;
				// values[0]:X軸,values[1]:Y軸,values[2]:Z軸
				float[] values = event.values;

				if (sensorType == Sensor.TYPE_ACCELEROMETER) {
					if ((Math.abs(values[0]) > shakeSenseValue
							|| Math.abs(values[1]) > shakeSenseValue || Math
							.abs(values[2]) > shakeSenseValue)) {
						// 觸發事件,執行打開應用行爲
						vibrator.vibrate(500);
						tvNum.setText(Generate.getNum(kind));

					}
				}
			}

		};

		// 4註冊偵聽事件
		mSensorManager.registerListener(sensorelistener,
				mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);

	}

這個界面中需要定義的成員變量

TextView tvKind;// 彩種文本
TextView tvChunyue;// 穿越文本
TextView tvNum;// 開獎號碼
Button btn_web;// 查看開獎結果
Button btn_wx;// 分享好友
String kind;


再來就是需要查看開獎結果,這時我們新建WebResultActivity,點擊按鈕跳轉到這個界面就可以了。其佈局文件中用到一個WebView

package com.xiaoshi.lottery;

import com.xiaoshi.lottery.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class WebResultActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		FullScreen();
		setContentView(R.layout.activity_web_result);
		int kindId = getIntent().getExtras().getInt("kind");
		WebView wv = (WebView) findViewById(R.id.webView1);
		// 設置WebView屬性,能夠執行Javascript腳本
		wv.getSettings().setJavaScriptEnabled(true);
		if (kindId == 1) {
			wv.loadUrl("http://www.lottery.gov.cn/");
		} else {
			wv.loadUrl("http://www.cwl.gov.cn/");
		}

	}

}

這時1.0版本基本成型了。

第二大步:推廣

如果你對自己的app比較自信,或者是同時想撈點辛苦費,這時您就可以考慮怎麼推廣自己的app,怎麼通過app賺外快了。

這裏我用到的一個簡單的推廣辦法就是微信推廣,朋友給朋友介紹的軟件安裝率應該才高麼。

首先在微信開放平臺申請賬號、註冊登錄->創建自己的應用->填寫相關信息->待審覈通過後->你就可以獲得該app對應的appkey等信息了。

實現代碼時首先在manifest中添加對應的權限(有些多餘的是爲了後期加廣告)

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

然後在自己的工程中新建包    自己程序包名+wxapi ,在此包下新建類WXEntryActivity,名字必須是這個。導入對應jar包,WXEntryActivity內容

package com.xiaoshi.lottery.wxapi;


import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
	private IWXAPI api;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		api = WXAPIFactory.createWXAPI(this, "wx****************caa", false);//第二個參數到時填自己的appkey
		api.handleIntent(getIntent(), this);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onResp(BaseResp resp) {
		// LogManager.show("TAG", resp.errCode: + resp.errCode + ,resp.errStr: +
		// resp.errStr, 1);
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			// 分享成功
			Log.e("test", "成功");
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			// 分享取消
			Log.e("test", "取消");
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			// 分享拒絕
			Log.e("test", "失敗");
			break;
		}
	}

	@Override
	public void onReq(BaseReq arg0) {
		// TODO 自動生成的方法存根

	}
}

我們在OpenActivity界面加入分享按鈕,

Button btn_wx;// 分享好友
private IWXAPI wxApi;
	public static final String WX_APP_ID = "wx********************aa";// 微信分享

// 實例化微信分享實例
		wxApi = WXAPIFactory.createWXAPI(this, WX_APP_ID);
		wxApi.registerApp(WX_APP_ID);


		// 分享好友
		btn_wx = (Button) findViewById(R.id.Button02);
		btn_wx.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {
				wechatShare(0);
			}
		});

//flag爲0時分享給好友或羣,flag爲1時分享到朋友圈
private void wechatShare(int flag) {
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = "http://shouji.baidu.com/software/item?docid=8554193&from=as";
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = "也沒啥好禮物,就送你500萬吧!";
		msg.description = tvKind.getText() + " "
				+ tvNum.getText() + " 。快去買吧,中了別忘了給我分點,呵呵!";
		// 這裏替換一張自己工程裏的圖片資源
		Bitmap thumb = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher);
		msg.setThumbImage(thumb);
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = String.valueOf(System.currentTimeMillis());
		req.message = msg;
		req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession
				: SendMessageToWX.Req.WXSceneTimeline;
		wxApi.sendReq(req);
		Log.e("test", "send");
	}

至此分享功能已實現。


第三大步:廣告

這裏我拿百度應用來舉例:首先在 百度應用開發者平臺註冊賬號,上傳自己應用。填寫相關信息,等審覈通過後。點擊獲取服務,在廣告裏添加廣告位,會生成對應的廣告id,自己應用id。

導入對應jar包,在自己的程序添加

private BDInterstitialAd appxInterstitialAdView;// 插屏廣告
	Handler myHandler;


// 處理廣告相應
		myHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 1:
					appxInterstitialAdView.showAd();
					break;
				case 2:
					appxInterstitialAdView.loadAd();
					break;
				}
			}
		};


private void loadAd() {
		// 創建廣告視圖
		// 發佈時請使用正確的ApiKey和廣告位ID
		appxInterstitialAdView = new BDInterstitialAd(this,
				"ekYuWDotdIQnLGhQgPDf4Rb0oFwFHfze", "Nx0bOLRLSWkoF1oWYgTo46Lx");
		// 設置插屏廣告行爲監聽器
		appxInterstitialAdView.setAdListener(new InterstitialAdListener() {

			String TAG = "ad";

			@Override
			public void onAdvertisementDataDidLoadFailure() {
				Log.e(TAG, "load failure");
			}

			@Override
			public void onAdvertisementDataDidLoadSuccess() {
				Log.e(TAG, "load success");
			}

			@Override
			public void onAdvertisementViewDidClick() {
				Log.e(TAG, "on click");
			}

			@Override
			public void onAdvertisementViewDidHide() {
				Log.e(TAG, "on hide");
			}

			@Override
			public void onAdvertisementViewDidShow() {
				Log.e(TAG, "on show");
			}

			@Override
			public void onAdvertisementViewWillStartNewIntent() {
				Log.e(TAG, "leave");
			}

		});

		// 加載廣告
		appxInterstitialAdView.loadAd();
		new Thread(new Runnable() {

			@Override
			public void run() {
				// 當廣告未加載時一直執行
				while (true) {
					try {
						Thread.sleep(3000);
						// 展示插屏廣告前先請先檢查下廣告是否加載完畢
						if (appxInterstitialAdView.isLoaded()) {
							Message msg = new Message();
							msg.what = 1;
							myHandler.sendMessage(msg);
							return;
						} else {
							Message msg = new Message();
							msg.what = 2;
							myHandler.sendMessage(msg);
							Log.i("ad", "AppX Interstitial Ad is not ready");
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

這時軟件基本完成。


希望倉促完成的本文會對您有所啓發,彩票助手app使用體驗可以點擊下面鏈接

app下載地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章