开发者

Starting Activity from a non-Activity in c2dm not showing

I am trying to display messages from C2DM so When I start an Activity from a non activity it works but only first time. My activity contains View and close button so if the user is not inside the app when he receive the push notification then it should start the app when he press view button or close it on close this works as well but when user press view button and enters the app after than I am unable to start the activity again, it seems like the activity is there but its not visible to user. My code is as below , hope you understand what I am saying. The C2DM code example used is taken from 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       
    }
}

MESSAGE DIALOG ACTIVITY 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();
        }
}

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> 

If i remove androidLaunchMode singleTop form manifest file and add set flag to multipleTask in C2DMReciver class it works but i get lots of dialog for each message. Thanks for help .


Set flag to singletop , the code to call messagedialogactivity is as under:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜