gpt4 book ai didi

java - 使用 Google Cloud Messaging 数据附加 TextView

转载 作者:太空宇宙 更新时间:2023-11-04 15:13:39 25 4
gpt4 key购买 nike

我想尝试使用 GCM 制作一些聊天应用程序。我尝试一步步学习,但感到困惑:

  • 如何在我的 ReceiveActivity 中附加 TextView
  • 如何在不点击来自 BroadcastReceiver 的通知的情况下在我的 Activity 中附加 TextView

这是我的 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());
}
}

这是我的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();
}
}

谢谢

最佳答案

哦,我的问题解决了。我尝试搜索如何使用 localBroadcast ,每当收到 GCM 数据时,它都会附加到我的 textView 中,而无需单击我的应用程序通知。我发现这篇文章对我很有帮助how to use LocalBroadcastManager?

关于java - 使用 Google Cloud Messaging 数据附加 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com