我在尝试更改位置时向GCM发送数据。 以下内容有效:
当我按下按钮时,我可以将数据发送到GCM。我使用这段代码:
public void sendMessageToGCMAppServer(final String status) {
new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... params) {
String msg = "";
try {
Bundle data = new Bundle();
Clock clock = new Clock();
String timestamp = clock.getTimeStamp();
data.putString("my_message", status + " - " + timestamp);
data.putString("my_action",
"com.google.android.gcm.demo.app.ECHO_NOW");
String id = Integer.toString(msgId.incrementAndGet());
gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);
msg = "Data sended to GCM";
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
//mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
}
但是当我尝试从我的locationService类向GCM发送数据时,我的程序崩溃了。
public void onLocationChanged(final Location loc)
{
Log.i("**************************************", "Location changed");
if(isBetterLocation(loc, previousBestLocation)) {
HomeActivity t = new HomeActivity();
t.sendMessageToGCMAppServer("Driving");
}
}
任何人都知道我错过了什么?