Java 使用Google云消息数据附加TextView
我想尝试使用GCM制作一些聊天应用程序。 我试着一步一步地学习,并困惑于: 如何在我的ReceiveActivity中附加TextView。 如何在我的活动中附加TextView而不点击BroadcastReceiver的通知? 这是我的gcminentservice.java,我希望这个类中的数据显示在ReceiveActivity中,而不点击通知Java 使用Google云消息数据附加TextView,java,android,google-cloud-messaging,Java,Android,Google Cloud Messaging,我想尝试使用GCM制作一些聊天应用程序。 我试着一步一步地学习,并困惑于: 如何在我的ReceiveActivity中附加TextView。 如何在我的活动中附加TextView而不点击BroadcastReceiver的通知? 这是我的gcminentservice.java,我希望这个类中的数据显示在ReceiveActivity中,而不点击通知 package com.fahri.runningchat;import android.app.IntentService; import an
package com.fahri.runningchat;import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GcmIntentService extends IntentService{
Context context;
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM Demo";
public GcmIntentService() {
super("GcmIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
//sendNotification("Received: " + extras.toString());
sendNotification(msg);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
//Intent myintent = new Intent(this, ReceiveActivity.class);
Intent myintent = new Intent(this, ReceiveActivity.class);
myintent.putExtra("message", msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
谢谢哦,我解决了我的问题。我尝试搜索如何使用localBroadcast,每当收到GCM数据时,它都会附加到我的textView,而无需单击我的应用程序通知。
我发现这篇文章对我很有帮助我会制作一些应用程序内部消息服务,通知片段/活动有一些新的数据/设置/需要处理的任何东西。这样,您就不必在广播接收器=D中保留对相关活动的引用
public class ReceiveActivity extends Activity {
JSONObject json;
EditText isian;
public static TextView pesan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
Intent intent = getIntent();
isian = (EditText) findViewById(R.id.editText2);
pesan= (TextView) findViewById(R.id.textViews);
String message = intent.getExtras().getString("message");
Log.e("message",message);
pesan.setText(message+"");
}
public void kirim(View view)
{
String url = "http://deaven.bl.ee/nyoba.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("message", isian.getText().toString()));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}