不过做起来倒是不是很难
Application ID
,REST API Key
,Access Key
等等。当然,不同的开放平台所提供的参数数量名称可能不太一样。但目的都是一个,就是将你的真实的app,绑定到这个云端来。好准确的给你提供服务。介绍就这么多了,主要还是参照开发文档来的。以下是代码区:
<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: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" >
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your Name"
android:inputType="textMultiLine" >
EditText>
<EditText
android:id="@+id/et_feedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_name"
android:hint="Your feedBack"
android:inputType="textMultiLine"
android:lines="3" >
EditText>
<Button
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_feedback"
android:onClick="submit"
android:text="submit" />
<Button
android:id="@+id/bt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bt1"
android:onClick="queryAll"
android:text="queryAll" />
<EditText
android:id="@+id/query_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/bt3"
android:layout_alignParentLeft="true"
android:layout_below="@id/bt2"
android:layout_toLeftOf="@+id/bt3"
android:hint="name" />
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/bt2"
android:onClick="queryFeedBack"
android:text="queryFeedBack" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/query_et"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:onClick="pushAll"
android:text="pushAll" />
RelativeLayout>
MainActivity
代码package com.duck.moocBmob;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import cn.bmob.push.BmobPush;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobInstallation;
import cn.bmob.v3.BmobPushManager;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
public class MainActivity extends Activity {
private EditText etName;
private EditText etQuery;
private EditText etFeedBack;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
Bmob.initialize(this, "08ddeaf079d64ae5c047a1f3e87e3006");
// 使用推送服务时的初始化操作
BmobInstallation.getCurrentInstallation(this).save();
// 启动推送服务
BmobPush.startWork(this, "08ddeaf079d64ae5c047a1f3e87e3006");
etName = (EditText) findViewById(R.id.et_name);
etFeedBack = (EditText) findViewById(R.id.et_feedback);
etQuery = (EditText) findViewById(R.id.query_et);
}
public void pushAll(View view){
BmobPushManager pushManager = new BmobPushManager(context);
pushManager.pushMessageAll("Test");
}
public void queryFeedBack(View view) {
String name = etQuery.getText().toString().trim();
BmobQuery bmobQuery = new BmobQuery();
bmobQuery.addWhereContains("name", name);
bmobQuery.findObjects(context, new FindListener() {
@Override
public void onSuccess(List list) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("QueryAll");
StringBuilder sb = new StringBuilder();
for (FeedBack feedBack : list) {
sb.append(feedBack.getName() + ": "
+ feedBack.getFeedBack() + "\n");
}
builder.setMessage(sb.toString());
builder.create().show();
}
@Override
public void onError(int code, String msg) {
vtoast("ERROR code:" + code + " |msg:" + msg);
}
});
}
public void queryAll(View view) {
BmobQuery query = new BmobQuery();
query.findObjects(context, new FindListener() {
@Override
public void onSuccess(List list) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("QueryAll");
StringBuilder sb = new StringBuilder();
for (FeedBack feedBack : list) {
sb.append(feedBack.getName() + ": "
+ feedBack.getFeedBack() + "\n");
}
builder.setMessage(sb.toString());
builder.create().show();
}
@Override
public void onError(int code, String msg) {
vtoast("ERROR code:" + code + " |msg:" + msg);
}
});
}
public void submit(View view) {
String name = etName.getText().toString().trim();
String feedBackStr = etFeedBack.getText().toString().trim();
if (name.equals("") || feedBackStr.equals("")) {
return;
}
FeedBack feedBack = new FeedBack();
feedBack.setName(name);
feedBack.setFeedBack(feedBackStr);
feedBack.save(context, new SaveListener() {
@Override
public void onSuccess() {
vtoast("success");
}
@Override
public void onFailure(int code, String msg) {
System.out.println("onFailure==code:" + code);
System.out.println("onFailure==msg:" + msg);
vtoast("failure");
}
});
}
protected void vtoast(String text) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
}
package com.duck.moocBmob;
import org.json.JSONException;
import org.json.JSONObject;
import cn.bmob.push.PushConstants;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BmobPushReceiver extends BroadcastReceiver {
@Override
@SuppressWarnings("deprecation")
public void onReceive(Context context, Intent intent) {
if (intent != null) {
if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
String text = intent
.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
try {
JSONObject jsonObject = new JSONObject(text);
String value = jsonObject.getString("alert");
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.ic_launcher, "Bmob Test",
System.currentTimeMillis());
notification.setLatestEventInfo(context, "Bmob Push",
value, null);
notificationManager.notify(R.drawable.ic_launcher,
notification);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.duck.moocBmob"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="cn.bmob.permission.push"
android:protectionLevel="normal" >
permission>
<uses-permission android:name="cn.bmob.permission.push" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.duck.moocBmob.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<service
android:name="cn.bmob.push.lib.service.PushService"
android:exported="true"
android:label="PushService"
android:permission="cn.bmob.permission.push"
android:process="cn.bmob.push" >
<intent-filter>
<action android:name="cn.bmob.push.lib.service.PushService" />
intent-filter>
service>
<receiver android:name="cn.bmob.push.PushReceiver" >
<intent-filter android:priority="2147483647" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
intent-filter>
receiver>
<receiver android:name="com.duck.moocBmob.BmobPushReceiver" >
<intent-filter>
<action android:name="cn.bmob.push.action.MESSAGE" />
intent-filter>
receiver>
application>
manifest>