话不多少 先看代码
public class MainActivity extends Activity {
private EditText edt1, edt2;
private Button btn1,btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1 = (EditText) findViewById(R.id.edt1);
edt2 = (EditText) findViewById(R.id.edt2);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
// 初始化SDK
Bmob.initialize(this, "c945e1b1070e900af132bc082d22aae8");
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name = edt1.getText().toString();
String password = edt2.getText().toString();
// TODO Auto-generated method stub
if (name.equals("") || password.equals("")) {
return;
}
FeedBack feedbackobj = new FeedBack();
feedbackobj.setName(name);
feedbackobj.setFeedback(password);
feedbackobj.save(getApplicationContext(),new SaveListener() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "y", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int arg0, String arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "N", Toast.LENGTH_SHORT).show();
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
});
}
}
用javabean来装数据 这里是用的FeedBack.java
代码如下
public class FeedBack extends BmobObject {
private String name;
private String feedback;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFeedback() {
return feedback;
}
public void setFeedback(String feedback) {
this.feedback = feedback;
}
}
提交数据到云数据库
注意使用前 需要导包
btn2.setOnClickListener(new OnClickListener() {
String str1 = edt3.getText().toString();
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
BmobQuery<FeedBack> query = new BmobQuery<FeedBack>();
//query.addWhereEqualTo("name", str);
query.findObjects(getApplicationContext(), new FindListener<FeedBack>() {
@Override
public void onError(int arg0, String arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "N", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(List<FeedBack> feedbacks) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("query");
String str = "";
for (FeedBack feedback : feedbacks) {
str += feedback.getName() + ":" + feedback.getFeedback() + "\n";
}
builder.setMessage(str);
builder.create().show();
}
});
}
});
public void pushAll(View view) {
BmobPushManager push = new BmobPushManager(getApplicationContext());
push.pushMessageAll("Test");
}
``
新建一个servers
``
public class MyPushMessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String Message = "";
if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
String msg = intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
JSONTokener jsontoken = new JSONTokener(msg);
try {
JSONObject object = (JSONObject) jsontoken.nextValue();
Message = object.getString("alert");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
NotificationManager manager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification();
manager.notify(R.drawable.ic_launcher,notification);
}
}
}
<service android:label="PushService" android:name="cn.bmob.push.lib.service.PushService" android:process="cn.bmob.push" android:exported="true">
<intent-filter>
<action android:name="cn.bmob.push.lib.service.PushService"/>
</intent-filter>
</service>
<receiver android:name="cn.bmob.push.PushReceiver" >
<intent-filter>
<!-- 系统启动完成后会调用 -->
<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.example.bmob.MyPushMessageReceiver">
<intent-filter >
<action android:name="cn.bmob.push.action.MESSAGE"/>
</intent-filter>
</receiver>
所需要调用的权限
<!-- 允许联网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 获取GSM(2g)、WCDMA(联通3g)等网络状态的信息 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 获取wifi网络状态的信息 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- 获取sd卡写的权限,用于文件上传和下载 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 允许读取手机状态 用于创建BmobInstallation -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--BmobSDK所需的权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--推送所需的权限-->
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />