注意事项:
1.移动游戏基地sdk为动态打包,即公司信息,计费点信息都是在申请计费点的时候提交到移动的,而文中提到的计费点索引是移动申请后台看到的真实计费点的后三位(由于此处属于商务负责,本人无法登陆后台所以无法提供截图,见谅)。
2.注意文中提到的启动游戏界面前先启动移动界面,退出游戏时显示移动的退出确认界面。
3.注意主界面需要显示的:
开始游戏,设置,帮助,关于,更多游戏,退出必有选项。
好了,继续贴代码:
AndroidManifest.xml配置:
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme">
- <activity
- android:name="com.example.blogforyd.MainActivity"
- android:label="@string/app_name" >
- </activity>
- <!-- 移动所需Activity注册-->
- <activity
- android:name="cn.emagsoftware.gamebilling.activity.GameOpenActivity"
- android:screenOrientation="sensor"
- android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
- //移动游戏基地需要启动游戏前先启动移动的闪屏界面,所以游戏主入口注册移到了这里
- <intent-filter>
- <actionandroid:nameactionandroid:name="android.intent.action.MAIN" />
- <categoryandroid:namecategoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- <intent-filter>
- <actionandroid:nameactionandroid:name="android.intent.action.CHINAMOBILE_OMS_GAME"/>
- <categoryandroid:namecategoryandroid:name="android.intent.category.CHINAMOBILE_GAMES"/>
- </intent-filter>
- </activity>
- <!--移动所需Activity注册-->
- </application>
- <!--移动所需权限注册 -->
- <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.INTERNET" />
- <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE">
- </uses-permission>
- <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
- <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE">
- </uses-permission>
- <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE">
- </uses-permission>
- <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.SEND_SMS" />
- <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
- <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.READ_CONTACTS"/>
- 注意:如果下边这行报错,clean一下项目就行了,可以无视
- <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.WRITE_APN_SETTINGS">
- </uses-permission>
- <!--移动所需权限注册 -->
MainActivity:游戏入口Activity
- package com.example.blogforyd;
- import cn.emagsoftware.gamebilling.api.GameInterface;
- importcn.emagsoftware.gamebilling.api.GameInterface.GameExitCallback;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.KeyEvent;
- import android.view.View;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(BundlesavedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //调用初始化移动支付,否则无法启动支付界面(此方法最好放到启动游戏的时候调用)
- GameInterface.initializeApp(this);
- findViewById(R.id.button1).setOnClickListener(
- newView.OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- //TODO Auto-generated method stub
- Fiapfiap = new Fiap(MainActivity.this);
- //需要购买什么道具传进去相应的index,此处传0为激活游戏
- fiap.android_Pay(0);
- }
- });
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEventevent) {
- // TODO Auto-generated methodstub
- if (keyCode ==KeyEvent.KEYCODE_BACK) {
- exit_game();
- }
- returnsuper.onKeyDown(keyCode, event);
- }
- // 退出游戏
- public void exit_game() {
- GameInterface.exit(MainActivity.this,new GameExitCallback() {
- @Override
- public voidonConfirmExit() {
- System.exit(0);
- }
- @Override
- public voidonCancelExit() {
- }
- });
- }
- // 是否开启音效
- public boolean is_open_music() {
- returnGameInterface.isMusicEnabled();
- }
- }
Fiap.java:移动支付逻辑文件
- //移动支付
- //2013-04-28
- package com.example.blogforyd;
- import cn.emagsoftware.gamebilling.api.GameInterface;
- importcn.emagsoftware.gamebilling.api.GameInterface.BillingCallback;
- import android.app.Activity;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.widget.Toast;
- public class Fiap {
- private Activity mActivity;
- private boolean isRepeat;
- private String charge_Point;
- public Fiap(Activity activity){
- mActivity = activity;
- }
- //index是三种计费点对应的标识
- public void android_Pay(int index){
- switch (index) {
- case 0:
- //激活游戏
- isRepeat =false;
- //此处计费点填写移动平台申请的计费点的后三位作为计费点索引
- charge_Point= "000";
- break;
- case 1:
- isRepeat =true;
- charge_Point= "001";
- break;
- case 2:
- isRepeat =true;
- charge_Point= "002";
- break;
- case 3:
- isRepeat =true;
- charge_Point= "003";
- break;
- default:
- break;
- }
- fiapHandler.sendEmptyMessage(1);
- }
- Handler fiapHandler = new Handler(){
- public voidhandleMessage(Message msg) {
- if (msg.what== 1) {
- BillingCallbackcallBack = new BillingCallback() {
- @Override
- publicvoid onBillingFail(String arg0) {
- //TODO Auto-generated method stub
- //此处表示支付失败
- Log.i("resultof pay", arg0);
- }
- @Override
- publicvoid onBillingSuccess(String arg0) {
- //TODO Auto-generated method stub
- //此处为支付成功逻辑
- Toast.makeText(mActivity,"支付成功", Toast.LENGTH_LONG).show();
- }
- @Override
- publicvoid onUserOperCancel(String arg0) {
- //TODO Auto-generated method stub
- //此处表示用户取消支付
- Log.i("resultof pay", arg0);
- }
- };
- //第一个参数为:触发计费接口的Context对象
- //第二个参数为:选择采用短信计费还是联网计费(true为短信,false为联网)
- //第三个参数为:计费点索引
- //第四个参数为:支付结果回调
- GameInterface.doBilling(mActivity,true, isRepeat, charge_Point,callBack);
- }
- };
- };
- }