Android 从c2dm中未显示的非活动启动活动

Android 从c2dm中未显示的非活动启动活动,android,Android,我试图显示来自C2DM的消息,所以当我从非活动启动活动时,它会工作,但只有第一次。“我的活动”包含“查看”和“关闭”按钮,因此,如果用户在收到推送通知时不在应用程序内,则应在他按下“查看”按钮时启动应用程序,或在“关闭”时关闭应用程序,这也可以工作,但当用户按下“查看”按钮并在之后进入应用程序时,我无法再次启动活动,看起来活动就在那里,但用户看不到它。我的代码如下,希望你明白我的意思。使用的C2DM代码示例取自 C2DM接收机 消息对话框活动类 AndroidManifest文件 如果我删除an

我试图显示来自C2DM的消息,所以当我从非活动启动活动时,它会工作,但只有第一次。“我的活动”包含“查看”和“关闭”按钮,因此,如果用户在收到推送通知时不在应用程序内,则应在他按下“查看”按钮时启动应用程序,或在“关闭”时关闭应用程序,这也可以工作,但当用户按下“查看”按钮并在之后进入应用程序时,我无法再次启动活动,看起来活动就在那里,但用户看不到它。我的代码如下,希望你明白我的意思。使用的C2DM代码示例取自

C2DM接收机

消息对话框活动类

AndroidManifest文件


如果我删除androidLaunchMode singleTop表单清单文件,并在C2DMReceiver类中将set标志添加到multipleTask中,它可以正常工作,但每个消息都会有很多对话框。感谢您的帮助。

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

Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);      
intent.setClass(context, MessageDialogActivity.class);
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();
        }
}
<?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> 
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);      
intent.setClass(context, MessageDialogActivity.class);