分类 android 下的文章

FaceBook SDK登录功能实现(Eclipse)


由于公司游戏要进行海外推广,所以要我们接入FBSDK 实现登录,分享,投放,所以写这篇文章,也算是个工作总结。
1、资料

(1).FB SDK github源码地址为
(2): [FB SDK中文接入文档]
2.创建应用,获取应用编号和密钥
这些事情,文档中都有详细描述,我就不进行赘述了
3.下载FB SDK,并配置参数
(1).解压facebook-android-sdk-4.25.0.aar文件,并将其中的assets,res,AndroidManifest.xml,classes.jar等复制的eclipse 新建工程下,并配置项目为library

(2).由于FB SDK引用了v7包和资源,所以要把v7项目引入到FB SDk中来(注:不只是v7包,项目一般在你的sdk目录下的extrasandroidsupportv7)

(3) 配置参数,在eclipse res/values/strings.xml中加入

<string name="facebook_app_id">133094273807591</string>

(4),在AndroidManifest中,添加权限,配置参数

<uses-permission android:name="android.permission.INTERNET"/>

application下配置参数

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

将FB SDK自定义Activity加到application中

<activity
    android:name="com.facebook.FacebookActivity"
    android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" /> 

(5).引入新建的FB sdk工程

4.接入 FB SDK

(1).在自定义application 中初始化SDK参数

public class MyApplication extends Application{

    public void onCreate() {
        super.onCreate();
        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
    }
}

(2),新建LoginActivity接入FB SDK代码

public class LoginActivity extends Activity {
    private static final String PERMISSION = "public_profile";

    private static final int PICK_PERMS_REQUEST = 0;

    private CallbackManager callbackManager;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     // 注册登录回调
        callbackManager = CallbackManager.Factory.create();
        LoginManager loginManager = LoginManager.getInstance();
        loginManager.setDefaultAudience(loginManager.getDefaultAudience());
        loginManager.setLoginBehavior(loginManager.getLoginBehavior());
        loginManager.logInWithReadPermissions(LoginActivity.this,
                Arrays.asList(PERMISSION));
        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(final LoginResult loginResult) {
                        // App code
                        Toast.makeText(LoginActivity.this, "登录成功",
                                Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(LoginActivity.this,
                                AppActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onCancel() {
                        // App code
                        Toast.makeText(LoginActivity.this, "登录失败,请稍后重试",
                                Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(final FacebookException exception) {
                        // App code
                        Toast.makeText(LoginActivity.this, "登录失败,请稍后重试",
                                Toast.LENGTH_LONG).show();
                    }
                });
      

    }

    /**
    * 处理登录结果
    */

    @Override
    protected void onActivityResult(final int requestCode,
            final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_PERMS_REQUEST) {
            if (resultCode == RESULT_OK) {
                LoginManager loginManager = LoginManager.getInstance();
                loginManager.setDefaultAudience(loginManager
                        .getDefaultAudience());
                loginManager.setLoginBehavior(loginManager.getLoginBehavior());
                loginManager.logInWithReadPermissions(LoginActivity.this,
                        Arrays.asList(PERMISSION));
            }
        } else {
            callbackManager.onActivityResult(requestCode, resultCode, data);
        }
    }
}

android发送短信


 使用android发送短信,需要先进行添加发送,读取短信新的权限:

   

 <uses-permission

android:name="android.permission.RECEIVE_SMS"/>

<uses-permission

android:name="android.permission.SEND_SMS"/>

 接着需要在界面添加一个两个EditText,用来添加发送的号码和发送的内容,接着再加一个button用来触发发送短信的操作,代码就不贴了,直接在可视化界面拖拽即可

  然后在Activity中获取到发送按钮对象,并重写其的setOnClickListener方法,代码如下:

 

Button send=(Button)findViewById(R.id.send);

   send.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                SmsManager smsManager=SmsManager.getDefault();
                Intent sendIntent=new Intent("SENT_SMS_ACTION");
                PendingIntent pi=PendingIntent.getBroadcast
                        (MainActivity.this, 0, sendIntent, 0);
                smsManager.sendTextMessage(发送的号码, null,
                        发送的内容, pi, null);
                
                
            }
        });

android handler传递数据


起因:在android使用get请求获取验证码时需要重开一个线程,这就造成了我无法获取到从服务器后台返回的数据

解决方法:创建全局变量,将返回的数据解析后返回给handler,再在handler中将数据赋值给全局变量

部分代码如下:

请输入代码private void getYzm(int money) {
    // TODO Auto-generated method stub
    String res = "";
    Date nowTime = new Date();
    SimpleDateFormat time = new SimpleDateFormat("yyMMddHHmm");
    String tel = PhoneUtils.getPhoneNumber(context);
    final String paramData = "imsi=" + imsi + "&imei=" + imei + "&tel="
            + tel + "&money=" + money + "&app="
            + app+ "&time=" + time.format(nowTime);
    String resultCode;
    
    new Thread(){
        public void run() {
            final String gainCode = HttpRequest.sendGet(url,
                    paramData);
        // url是请求验证码地址,是一个全局变量,这里没有列出来,paramData是请求参数,HttpRequest是一个工具类,下面发链接
            String resultCode;
            JSONObject dataJson;
            String orderid;
            try {
                dataJson = new JSONObject(gainCode);
                resultCode = dataJson.getString("resultCode");
                orderid=dataJson.getString("orderid");
                if("200000".equals(resultCode)){
                    msg = handler.obtainMessage();
                    Bundle bundle = new Bundle();
                    bundle.putString("orderid", orderid);// 将服务器返回的订单号传到Bundle中,,再通过handler传出
                    msg.setData(bundle);
                    msg.arg1 = 0;   // 0为获取验证码成功
                    handler.sendMessage(msg);
                }else{
                    msg = handler.obtainMessage();
                    msg.arg1 = 1;  // -1为获取验证码失败
                    handler.sendMessage(msg);  
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }.start();
    
}
class MsgHandler extends Handler {
     private Activity activity;  
        public MsgHandler(Activity activity){  
            this.activity=activity;
        }
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        switch (msg.arg1) {  
        case 0:  
            dialog.show();
            Bundle bundle=msg.getData();
            orderid=bundle.getString("orderid");// 这里的orderid是一个全局变量
            break; 
        case 1:  
            showInfo("获取验证码失败!");  
            break;
        case 2:  
            showInfo("订购成功!");  
            break;
        case 3:  
            showInfo("提交验证码错误,请重新测试!");  
            break;  
        default:  
            break;  
        }  
        super.handleMessage(msg);
    }