2014-01-11 72 views
1

我想嘗試使用GCM製作一些聊天應用程序。 我嘗試循序漸進地學習和困惑於:使用Google Cloud Messaging數據附加TextView

  • 如何在我的ReceiveActivity追加TextView
  • 如何在我的Activity中追加TextView而不從BroadcastReceiver中獲取通知?

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

謝謝

+0

我會做某種應用程序內部消息傳遞服務,通知片段/活動有一些新的數據/設置/需要處理的任何內容。這樣,您不必在廣播接收器= D中保留對有問題的活動的引用 –

回答

0

哦,我解決我的問題。我嘗試搜索如何使用localBroadcast,並且每當GCM數據收到時,它都會附加到我的textView,而無需點擊我的應用程序通知。 我發現這篇文章對我來說非常有用how to use LocalBroadcastManager?

相關問題