从c2dm中的非活动开始活动未显示

时间:2011-09-07 10:01:47

标签: android

我正在尝试显示来自C2DM的消息,所以当我从非活动开始一个活动时,它只能工作但是第一次。我的活动包含查看和关闭按钮,因此如果用户在收到推送通知时不在应用程序内,那么当他按下查看按钮或关闭它时应该启动应用程序这也可以,但是当用户按下查看按钮并进入时应用程序之后,我无法再次启动活动,似乎活动在那里,但用户不可见。我的代码如下,希望你理解我在说什么。使用的C2DM代码示例取自here

C2DMReceiver

package com.mks.android.example;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.android.c2dm.C2DMBaseReceiver;
import com.google.android.c2dm.C2DMessaging;
import com.mks.android.example.activity.MessageDialogActivity;

public class C2DMReceiver extends C2DMBaseReceiver {
    private static String senderId = "mysenderemail@gmail.com";
    String registrationId;
    public C2DMReceiver() {
        super(senderId);
    }

    @Override
    public void onRegistrered(Context context, String registrationId) {
        this.registrationId = registrationId;
        Log.e("Error", registrationId);
    }

    @Override
    public void onUnregistered(Context context) {
        C2DMessaging.register(context, senderId);
    }

    @Override
    public void onError(Context context, String errorId) {
        Log.w("Error", errorId);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i("Error", "MessageReceived");
        Bundle bundle = intent.getExtras();
        intent = new Intent();
        intent.putExtra("messageBundle", bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);       
        intent.setClass(context, MessageDialogActivity.class);
        startActivity(intent);      
        // load message display activity here       
    }
}

消息对话活动类

package com.mks.android.example.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.mks.android.example.R;
import com.mks.android.example.activity.list.Functions;

public class MessageDialogActivity extends Activity {
    private Functions func = new Functions();
    private int isAppActive = 0;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showMessages(); 
    }
    private void showMessages(){
        Bundle bundle = getIntent().getBundleExtra("messageBundle");
        String title = bundle.getString("title");
        String message = bundle.getString("message");
        setContentView(R.layout.c2dm_message);

        TextView txtTitle = (TextView) findViewById(R.id.title);
        TextView txtMessage  = (TextView) findViewById(R.id.message);
        Button btnView = (Button) findViewById(R.id.btnView);
        Button btnClose = (Button) findViewById(R.id.btnClose);
        txtTitle.setText(title);
        txtMessage.setText(message);

        this.isAppActive = func.getAppState(this);

        if(isAppActive==1){
            btnView.setVisibility(View.GONE);
        }       

        btnView.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
                if(isAppActive == 0){
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.setComponent(new ComponentName("com.mks.android.example","com.mks.android.example.activity.MainActivity"));
                    startActivity(intent);                  
                }
                MessageDialogActivity.this.finish();
            }
        });

        btnClose.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();               
            }
        });
    }
     @Override
        protected void onNewIntent(Intent intent) {
            setIntent(intent);
            showMessages();
        }
}

AndroidManifest FIle

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mks.android.example"
      android:versionCode="1"
      android:versionName="1.0" android:installLocation="preferExternal">
    <uses-sdk android:minSdkVersion="4" />
    <application android:label="@string/app_name" android:icon="@drawable/icon_launcher" android:debuggable="true">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".activity.MainActivity" 
                  android:label="@string/app_name"
                  android:screenOrientation="portrait"
                  android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".activity.MessageDialogActivity" android:theme="@style/DialogNoTitle" android:activity android:launchMode="singleTop" />

      <service android:name=".C2DMReceiver" />           
      <!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it --> 
      <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
          <!-- Receive the actual message -->
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.RECEIVE" />
              <category android:name="com.mks.android.example" />
          </intent-filter>
          <!-- Receive the registration id -->
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
              <category android:name="com.mks.android.example" />
          </intent-filter>
      </receiver>
    </application>
    <supports-screens android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true" />
<permission android:name="com.mks.android.example.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.mks.android.example.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest> 

如果我删除androidLaunchMode singleTop表单清单文件并将set flag添加到C2DMReciver类中的multipleTask它可以工作,但我得到了很多对话框的每个消息。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

将标志设置为singletop,调用messagedialogactivity的代码如下:

Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);      
intent.setClass(context, MessageDialogActivity.class);