[英]Append TextView Using Google Cloud Messaging data
I wanna try make some chat apps using GCM. 我想尝试使用GCM制作一些聊天应用程序。 I try to learn step by step and confused on:
我尝试逐步学习并感到困惑:
TextView
in my ReceiveActivity
. ReceiveActivity
追加TextView
。 TextView
in my Activity
without tapping the notification from BroadcastReceiver
? BroadcastReceiver
通知的情况下将TextView
附加到我的Activity
? This is my GCMIntentService.java
, I want the data from this class shown in ReceiveActivity
without tapping the notification 这是我的
GCMIntentService.java
,我希望来自该类的数据显示在ReceiveActivity
而不点击通知
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());
}
}
And this is my ReceiveActivity
, I want the message from server directly appended to the TextView
without tapping the notification. 这是我的
ReceiveActivity
,我希望来自服务器的消息直接附加到TextView
而不点击通知。
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();
}
}
Thank You 谢谢
Oh, I solved my problem. 哦,我解决了我的问题。 I try to search how to use localBroadcast and whenever GCM data receive, it will append to my textView without clicking my app notification.
我尝试搜索如何使用localBroadcast,并且只要收到GCM数据,它就会追加到我的textView中,而无需单击我的应用程序通知。 I found this post is very helpfull for me how to use LocalBroadcastManager?
我发现这篇文章对我如何使用LocalBroadcastManager很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.