android从另一个类

时间:2016-03-28 11:11:26

标签: android google-cloud-messaging

我有使用php的GCM和我的代码没有任何问题 它的想法从php获取通知并在通知栏上显示 当点击通知时,它会在TextView上显示消息 我只是想在没有点击通知的情况下更新活动上的TextView

现在让我展示一下了解我的代码 这个家庭活动包含TextView

   import android.app.Activity;
   import android.os.Bundle;
   import android.widget.TextView;


   public class Home_Activity extends Activity {
   TextView view_msg;
 // i try make it public static TextView view_msg;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    view_msg = (TextView) findViewById(R.id.message);
   }
   public void onResume()
   {
    super.onResume();
    String str = getIntent().getStringExtra("msg");  
    if (str != null) {
        view_msg.setText(str);  
    }
   } 
   }

我们可以看到它的

  

的onResume

检查是否有任何额外数据但我想在新gcm消息重新启动时更新该TextView

现在是重新发送消息的代码

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.medo.alex.Home_Activity;
import com.medo.alex.R;
import com.google.android.gms.gcm.GoogleCloudMessaging;


 public class Gcm_Notification_Intent_Service extends IntentService {
// Sets an ID for the notification, so it can be updated
public static final int notifyID = 9001;

public Gcm_Notification_Intent_Service() {
    super("GcmIntentService");
}


@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    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());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {

            sendNotification(""    +    extras.get(Gcm_Application_Constants.MSG_KEY)); //When Message is received normally from GCM Cloud Server
        }
    }
    Gcm_Broadcast_Receiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {
    Intent resultIntent = new Intent(this, Home_Activity.class);
    resultIntent.putExtra("msg", msg);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
            resultIntent, PendingIntent.FLAG_ONE_SHOT);  

    NotificationCompat.Builder mNotifyBuilder;
    NotificationManager mNotificationManager;

    mNotificationManager = (NotificationManager)      getSystemService(Context.NOTIFICATION_SERVICE);

    mNotifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("NEW MESSAGE")
            .setSmallIcon(R.mipmap.ic_launcher);
    // Set pending intent
    mNotifyBuilder.setContentIntent(resultPendingIntent);

    // Set Vibrate, Sound and Light
    int defaults = 0;
    defaults = defaults | Notification.DEFAULT_LIGHTS;
    defaults = defaults | Notification.DEFAULT_VIBRATE;
    defaults = defaults | Notification.DEFAULT_SOUND;

    mNotifyBuilder.setDefaults(defaults);
    // Set the content for Notification
    mNotifyBuilder.setContentText("YOU HAVE NEW MESSAGE");
    // Set autocancel
    mNotifyBuilder.setAutoCancel(true);
    // Post a notification
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());


  // MainActivity.view_msg.setText(msg); // iam try use this and make my view_msg public and static when i add this line and run my project and say progect name has stopped just when add this line 
  }
 }

现在我的问题是如何更新view_msg及其在Home_activity上的问题 当从Gcm中重新发送messgae时我会在代码中写下我尝试做什么,但是它说我的项目已停止现在忘记了我的尝试并且只是让我知道如何从另一个类更新view_msg TextView

1 个答案:

答案 0 :(得分:1)

您似乎需要Service bounding

一些很棒的例子,你可以找到HERE

只需尝试绑定您的Activity即可,只需发送内容Message即可。当Activity丢失时,例如保持Notification弹出