39

ここにあるのは、ロードされると進行中の通知が自動的に表示される単純な webview アクティビティです。アイデアは、ドロップダウン メニューをプルダウンして選択することで、ユーザーがこのアクティビティから離れて、必要な画面からすばやくアクセスできるようにすることです。次に、必要に応じて、メニューボタンを押して終了を押して通知を閉じるだけで、通知がクリアされます。これはすべてうまくいきます。ただし、通知が押されると、アクティビティの新しいインスタンスが開始されます。アクティビティがまだ破棄されていないかどうかを確認するには、何を変更する必要があり、そのインスタンスをコールバック (再開) するだけでよいため、再度ロードする必要がなく、スタックに別のアクティビティを追加する必要がありません。 . 何か案は?どんな助けでも大歓迎です。

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);
    }
}

@コモンズウェア

私が正しいことを確認するために、これはあなたが提案していたことですか?

このセリフがちょっと気になったのですが、

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...");        
}
4

5 に答える 5

55

アイデアは、ドロップダウン メニューをプルダウンして選択することで、ユーザーがこのアクティビティから離れて、必要な画面からすばやくアクセスできるようにすることです。

これは任意にしてください。

ただし、通知が押されると、アクティビティの新しいインスタンスが開始されます。

それはデフォルトで起こります。

アクティビティがまだ破棄されていないかどうかを確認するには、何を変更する必要がありますか?そのインスタンスをコールバック(再開)するだけでよいため、再度ロードする必要がなく、スタックに別のアクティビティを追加する必要がありません.

を取り除きFLAG_ACTIVITY_NEW_TASKます。追加notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);-このサンプル プロジェクトを参照してください。

Context context = getApplicationContext();

これをしないでください。Activityとして使用するだけContextです。

于 2010-07-22T02:00:10.647 に答える
21

他の誰かが私と同じ困難を抱えている場合に備えて....

上記のすべてのコードを試してみましたが成功しませんでした。通知項目は、既に実行されているアプリのまったく新しいインスタンスを起動し続けました。(一度に 1 つのインスタンスのみを使用できるようにしたいのです。) 最後に、この別のスレッド ( https://stackoverflow.com/a/20724199/4307281 ) に気付きました。これには、マニフェストに 1 つの単純な追加エントリが必要でした:

android:launchMode="singleInstance"

アプリケーションのメイン アクティビティ セクションの下にあるマニフェストにそれを配置すると、通知アイテムは既に進行中のインスタンスを再利用しました。

于 2015-04-01T19:29:08.297 に答える
2

どの解決策もうまくいかなかったので、自分のやり方を見つけました。私の活動はすべてBaseActivity. 簡単な一連の手順に従って、プッシュ通知をクリックしてアプリケーションを再開させました。

ステップ 1 : 現在のアクティビティを記録するための定数:

public static Context currentContext;

ステップ 2 :によって拡張された活動内onCreate()または活動のonResume()BaseActivity

currentContext = this;

ステップ 3 :Intent通知の作成時:

Class activity = BaseActivity.commonContext.getClass();
Intent intent = new Intent(this, activity);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

//FLAG_UPDATE_CURRENT is important
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
于 2016-09-14T04:26:13.740 に答える