如何使通知意向的简历,而不是做一个新的意图是什么? [英] How to make notification intent resume rather than making a new intent?

查看:121
本文介绍了如何使通知意向的简历,而不是做一个新的意图是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里是一个简单的WebView活动,当加载它自动显示一个正在进行的通知。我们的想法是,人们可以从这项活动导航离开,并迅速拉下下拉菜单并选择从任何他们想要的画面再次访问它。然后,当他们想要的,他们可以通过点击菜单按钮,击中退出,然后通知清除只需关闭通知。这一切工作正常。然而,当通知是pressed它启动活性的新实例。那么我不得不改变,使其看是否该活动尚未破坏,我可以调用该实例回(恢复它),因此无需再装回,不需要另一个活动添加到我的筹码。有任何想法吗?任何帮助将大大AP preciated。

 包com.my.app;

进口com.flurry.android.FlurryAgent;

进口android.app.Activity;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.app.ProgressDialog;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.KeyEvent;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.webkit.CookieSyncManager;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;
进口android.widget.Toast;

公共类聊天延伸活动{
    私人ProgressDialog进度;
    公众的WebView的WebView;
    私有静态最后字符串变量=主;

    私人NotificationManager mNotificationManager;
    私人诠释SIMPLE_NOTFICATION_ID;

    @覆盖
    公共无效的OnStart(){
       super.onStart();
       CookieSyncManager.getInstance()同步()。
       FlurryAgent.onStartSession(这一点,H9QGMRC46IPXB43GYWU1);
    }

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.chat);

        mNotificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        最后通知notifyDetails =新的通知(R.drawable.chat_notification,聊天开始,System.currentTimeMillis的());

        notifyDetails.flags | = Notification.FLAG_ONGOING_EVENT;

        上下文的背景下= getApplicationContext();

        CharSequence的contentTitle =聊天;
        CharSequence的contentText =preSS返回聊天;

        意图notifyIntent =新的意图(背景下,Chat.class);

        PendingIntent意图=
        PendingIntent.getActivity(Chat.this,0,
        notifyIntent,android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        notifyDetails.setLatestEventInfo(背景下,contentTitle,contentText,意图);

        mNotificationManager.notify(SIMPLE_NOTFICATION_ID,notifyDetails);

        CookieSyncManager.createInstance(本);
        。CookieSyncManager.getInstance()startSync();
        的WebView =(web视图)findViewById(R.id.webviewchat);
        webview.setWebViewClient(新chatClient());
        webview.getSettings()setJavaScriptEnabled(真)。
        webview.getSettings()setPluginsEnabled(真)。
        webview.loadUrl(http://google.com);

        进度= ProgressDialog.show(Chat.this,,加载聊天......);
    }

    私有类chatClient扩展WebViewClient {
        @覆盖
        公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
            Log.i(TAG,处理的WebView URL点击......);
            view.loadUrl(URL);
            返回true;
        }

        公共无效onPageFinished(web视图查看,字符串URL){
            Log.i(TAG,加载完成URL:+网址);
            如果(progressBar.isShowing()){
                progressBar.dismiss();
            }
        }
    }

    公共布尔的onkeydown(INT键code,KeyEvent的事件){
        如果((钥匙code == KeyEvent.KEY code_BACK)及和放大器; webview.canGoBack()){
            webview.goBack();
            返回true;
        }
        返回super.onKeyDown(键code,事件);
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        super.onCreateOptionsMenu(菜单);
        MenuInflater充气= getMenuInflater();
        inflater.inflate(R.menu.chatmenu,菜单);
        返回true;
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
            案例R.id.home:
                意图=新的意图(这一点,Home.class);
                startActivity(一);
                返回true;
            案例R.id.closechat:
                mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
                意向V =新的意图(这一点,Home.class);
                startActivity(五);
                返回true;
        }
        返回false;
    }

    公共无效的onStop(){
       super.onStop();
       CookieSyncManager.getInstance()同步()。
       FlurryAgent.onEndSession(本);
    }
}
 

@Commonsware

只是,以确保我有它正确的,这算什么,你是在暗示什么?

我正要这一行,

有点担心

  PendingIntent.getActivity(Chat.this,0,notifyIntent,SIMPLE_NOTFICATION_ID);

公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.chat);

    mNotificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    最后通知notifyDetails =新的通知(R.drawable.chat_notification,聊天开始,System.currentTimeMillis的());

    notifyDetails.flags | = Notification.FLAG_ONGOING_EVENT;


    上下文的背景下= getApplicationContext();

    CharSequence的contentTitle =聊天;
    CharSequence的contentText =preSS返回聊天;

    意图notifyIntent =新的意图(背景下,Chat.class);

    PendingIntent意图=
    PendingIntent.getActivity(Chat.this,0,notifyIntent,SIMPLE_NOTFICATION_ID);
    notifyDetails.setLatestEventInfo(背景下,contentTitle,contentText,意图);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    mNotificationManager.notify(SIMPLE_NOTFICATION_ID,notifyDetails);


    CookieSyncManager.createInstance(本);
    。CookieSyncManager.getInstance()startSync();
    的WebView =(web视图)findViewById(R.id.webviewchat);
    webview.setWebViewClient(新chatClient());
    webview.getSettings()setJavaScriptEnabled(真)。
    webview.getSettings()setPluginsEnabled(真)。
    webview.loadUrl(http://google.com);

    进度= ProgressDialog.show(Chat.this,,加载聊天......);
}
 

解决方案
  

的想法是,人们可以导航   距此活动,并快速   在任何屏幕上再次访问它,他们   想拉下下拉   菜单,然后选择它。

请让这个可选的。

  

然而,当通知是   pressed它开始的一个新实例   的活性。

这是在默认情况下发生的。

  

那么我不得不改变,使其   看到,如果活动有尚未   被摧毁,我可以叫   该实例回来(重新开始吧)   因此无需重新装入   并且不需要添加其他活动   以我的筹码。

摆脱 FLAG_ACTIVITY_NEW_TASK 的。添加 notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); - 看的此示例项目

 上下文语境= getApplicationContext();
 

请不要这样做。只需使用你的活动上下文

What i have here is a simple webview activity that when loaded it auto displays an ongoing notification. The idea is that people can navigate away from this activity and quickly access it again from any screen they want by pulling down the drop down menu and selecting it. Then when they want they can just close the notification by hitting the menu button and hitting exit and then the notification clears. This all works fine. However, when the notification is pressed it starts a new instance of the activity. What would i have to change to make it see if the activity has not already been destroyed and i can just call that instance back(resume it) and therefore not needing to load it again and won't need to add another activity to my stack. Any ideas? Any help would be greatly appreciated.

package com.my.app;

import com.flurry.android.FlurryAgent;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent; 
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Chat extends Activity { 
    private ProgressDialog progressBar;
    public WebView webview;
    private static final String TAG = "Main";

    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;

    @Override
    public void onStart() {
       super.onStart();
       CookieSyncManager.getInstance().sync();
       FlurryAgent.onStartSession(this, "H9QGMRC46IPXB43GYWU1");
    }

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat);

        mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        final Notification notifyDetails = new Notification(R.drawable.chat_notification,"Chat Started",System.currentTimeMillis());

        notifyDetails.flags |= Notification.FLAG_ONGOING_EVENT;

        Context context = getApplicationContext();

        CharSequence contentTitle = "Chat";
        CharSequence contentText = "Press to return to chat";

        Intent notifyIntent = new Intent(context, Chat.class);

        PendingIntent intent =
        PendingIntent.getActivity(Chat.this, 0,
        notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);

        mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);

        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().startSync();
        webview = (WebView) findViewById(R.id.webviewchat);
        webview.setWebViewClient(new chatClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setPluginsEnabled(true);
        webview.loadUrl("http://google.com");

        progressBar = ProgressDialog.show(Chat.this, "", "Loading Chat...");  
    }

    private class chatClient extends WebViewClient { 
        @Override 
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i(TAG, "Processing webview url click...");
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            Log.i(TAG, "Finished loading URL: " +url);
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 
            webview.goBack(); 
            return true; 
        }
        return super.onKeyDown(keyCode, event); 
    }

    @Override
    public boolean onCreateOptionsMenu (Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.chatmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
        switch (item.getItemId()) {
            case R.id.home:
                Intent a = new Intent(this, Home.class);
                startActivity(a);
                return true;
            case R.id.closechat:
                mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
                Intent v = new Intent(this, Home.class);
                startActivity(v);
                return true;
        }
        return false;
    }

    public void onStop() {
       super.onStop();
       CookieSyncManager.getInstance().sync();
       FlurryAgent.onEndSession(this);
    }
}

@Commonsware

Just to be sure i have it correct, is this what you were suggesting?

I was a little worried about this line,

PendingIntent.getActivity(Chat.this, 0, notifyIntent, SIMPLE_NOTFICATION_ID);

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat);

    mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    final Notification notifyDetails = new Notification(R.drawable.chat_notification,"Chat Started",System.currentTimeMillis());

    notifyDetails.flags |= Notification.FLAG_ONGOING_EVENT;


    Context context = getApplicationContext();

    CharSequence contentTitle = "Chat";
    CharSequence contentText = "Press to return to chat";

    Intent notifyIntent = new Intent(context, Chat.class);

    PendingIntent intent =
    PendingIntent.getActivity(Chat.this, 0, notifyIntent, SIMPLE_NOTFICATION_ID);
    notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);


    CookieSyncManager.createInstance(this);
    CookieSyncManager.getInstance().startSync();
    webview = (WebView) findViewById(R.id.webviewchat);
    webview.setWebViewClient(new chatClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setPluginsEnabled(true);
    webview.loadUrl("http://google.com");

    progressBar = ProgressDialog.show(Chat.this, "", "Loading Chat...");        
}

解决方案

The idea is that people can navigate away from this activity and quickly access it again from any screen they want by pulling down the drop down menu and selecting it.

Please make this optional.

However, when the notification is pressed it starts a new instance of the activity.

That will happen by default.

What would i have to change to make it see if the activity has not already been destroyed and i can just call that instance back(resume it) and therefore not needing to load it again and won't need to add another activity to my stack.

Get rid of FLAG_ACTIVITY_NEW_TASK. Add notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); -- see this sample project.

Context context = getApplicationContext();

Please don't do this. Just use your Activity as a Context.

这篇关于如何使通知意向的简历,而不是做一个新的意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆