点击通知在android中输入我的应用程序

目前我正在开发GCM(Google Cloud消息),它允许用户将消息推送到用户设备.我想达到以下要求:

>如果用户已输入app,请忽略它
>如果用户未输入应用程序,请单击通知以进入应用程序

我的应用程序的工作流程是:

> WelcomePage(从中下载json并创建数据集)=> MainPage(基于数据集显示)

处理通知代码

private void sendNotification(String msg) {
        mnotificationmanager = (notificationmanager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        String notifyMsg = "";
        JSONTokener tokener = new JSONTokener(msg);

        if (tokener != null) {
            try {
                notifyMsg = new JSONObject(tokener).getString("msg");
            } catch (JSONException e) {
                // Todo Auto-generated catch block
                e.printstacktrace();
            }
        }

        Intent myintent = new Intent(this,WelcomePageActivity.class);
        myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this,myintent,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(getResources().getString(R.string.notification_title))
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(notifyMsg))
        .setContentText(notifyMsg)
        .setContentIntent(contentIntent);

        mnotificationmanager.notify(NOTIFICATION_ID,mBuilder.build());
    }

问题是如果我使用WelcomePageActivity类,如果我在主页面,它将创建一个新活动,我如何调整代码以满足我的要求?

谢谢

解决方法

对于
1.如果用户已经输入应用程序,请忽略它:
在onReceive()中,检查您的应用是否正在运行,不要通知.
可以通过以下方式检查:
ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);

if((serviceList.size() > 0)) {
    boolean found = false;

    for(int i = 0; i < serviceList.size(); i++) {
        RunningServiceInfo serviceInfo = serviceList.get(i);
        ComponentName serviceName = serviceInfo.service;

        if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
            //Your service or activity is running
            break;
        }
    }

>如果用户未输入应用程序,请单击通知以进入应用程序
从上面的代码中,您知道是否要恢复应用程序或启动 – 调用启动画面或您的案例WelcomeActivity.

关于您的应用程序的工作流程,我建议您检查是否需要每次都下载数据.可以保存它或仅在需要时更新/下载,其余流程按原样工作.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...