2011-07-28 4 views
5

Androidアプリを開発中で、ユーザーがクリアできないステータスバー通知が必要です。誰もそれを行う方法を知っていますか?私はSkimbleのようなアプリケーションで、アプリケーションを使用しているときにクリアできない通知が表示されるところを見ました。Androidステータスバー通知:アンパック可能にしてアプリに戻す(新しいインスタンスを開始しない)

また、ユーザーが通知をクリック/押下したときに、既に実行中のアプリケーションインスタンスに戻ることができます。今すぐ新しいインスタンスを開始します。もう一度Skimbleアプリのように、私はちょうどすでに実行されているアプリのインスタンスに戻りたいと思う。

ありがとうございました。

答えて

4

この問題の解決方法が見つかりました。 FLAG_ONGOING_EVENTを使用して、通知を持続させました。ここでは、コードです:あなたがNotification.builderを使用する必要がありますし、あなたの使って2.Xと下位API場合は、NotificationComacpt.Builderを使用する必要があり、これが適用されないと私はcouldnよう

String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

    int icon = R.drawable.mypic; 
    long when = System.currentTimeMillis(); 
    Notification notification = new Notification(icon, tickerText, when); 
    notification.flags = Notification.FLAG_ONGOING_EVENT; 

    Context context = getApplicationContext(); 
    CharSequence contentTitle = "Title Text"; 
    CharSequence contentText = "Content text."; 

    Intent intent = new Intent(this, MyClass.class); 
    intent.setAction("android.intent.action.MAIN"); 
    intent.addCategory("android.intent.category.LAUNCHER"); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

    final int HELLO_ID = 1; 
    mNotificationManager.notify(HELLO_ID, notification); 
+0

'FLAG_ONGOING_EVENT'を使用する代わりに、通知を表示する場所に応じて' FLAG_NO_CLEAR'を使用することもできます(詳細はこちら)(http://developer.android.com/reference/android/app /Notification.html#FLAG_NO_CLEAR)) – THelper

0

はnotification.setLatestEventInfoが廃止されました私の通知を残すことはありません。私はsetDeleteIntentを使い、同じアクティビティを指していて、レジュームメソッドでは、あなたの通知を示すコードのブロックを呼び出します。だから、それはまあまあ仕事です。ここで

3

Google docsNotificationCompat.Builder

NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("MyApp") .setContentText("MyMsg") .setOngoing(true);

詳細のためのソリューションです。

関連する問題