<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 这里就一个MainActivity的xml,没有别的。下面是
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<FeedBack> bmobQuery = new BmobQuery<FeedBack>();
bmobQuery.addWhereContains("name", name);
bmobQuery.findObjects(context, new FindListener<FeedBack>() {
@Override
public void onSuccess(List<FeedBack> 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<FeedBack> query = new BmobQuery<FeedBack>();
query.findObjects(context, new FindListener<FeedBack>() {
@Override
public void onSuccess(List<FeedBack> 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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 然后是推送消息接收器的代码,也就是一个广播接收者:
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()
}
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 关于通知栏的显示,就是在广播接收这里做的。
- 因为这里面涉及到一些权限的使用,以及lib库里面一些组建的注册,于是也贴出 清单文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 以上,就是全部代码了。
- 需要注意的是:一定要导入Bmob 提供的lib库。不然上面的全是白搭。