2016-09-06 36 views
10

我想創建一個小窗口,如果您點擊通知上的快速回覆按鈕,可以打開一個小窗口。在WhatsApp中,它打開了一個半屏窗口。目前我正在做以下幾點:試圖爲前N個手機創建Whatsapps快速回復

我開闢了一個叫NotificationActivity的活動。在AndroidManifest.xml我註冊的活動作爲

<activity 
    android:name=".activity.NotificationActivity" 
    android:theme="@style/Theme.AppCompat.Light.Dialog.custom" 
    android:label="@string/title_activity_notification" 
    android:screenOrientation="portrait" 
    android:windowSoftInputMode="adjustResize|stateHidden" /> 

這是風格:

<style name="Theme.AppCompat.Light.Dialog.custom"> 
    <item name="windowNoTitle">true</item> 
    <item name="windowActionBar">false</item> 
</style> 

應用程序是完全封閉的現在,當(關閉,然後滑開的)它完美的作品。

但是,如果應用程序被最小化,當有人點擊回覆按鈕時,它會打開應用程序,然後將NotificationActivity粘貼到應用程序上。如何防止應用程序在後臺打開,並且只打開半屏通知活動。

非常感謝

編輯:我在想,也許是xml文件是相關的?

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="300dp" 
    android:layout_marginTop="15dp" 
    android:background="@color/white" 
    android:orientation="vertical" 
    android:paddingTop="12dp" 
    android:weightSum="20"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/lvChat" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="10" 
      android:background="@android:color/transparent" 
      android:cacheColorHint="@android:color/transparent" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="0dp" 
      android:listSelector="@android:color/transparent" 
      android:scrollbars="none" 
      android:stackFromBottom="false" 
      android:transcriptMode="alwaysScroll"/> 

     <LinearLayout 
      android:id="@+id/chatFooter" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#ECEFF1" 
      android:orientation="horizontal"> 

      <LinearLayout 
       android:id="@+id/sendLayout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center_vertical" 
       android:paddingBottom="@dimen/scale_5dp" 
       android:paddingTop="@dimen/scale_5dp" 
       android:weightSum="2"> 

       <LinearLayout 
        android:layout_width='0dp' 
        android:layout_height="wrap_content" 
        android:layout_weight="1.8"> 

        <com.heyjude.heyjudeapp.customview.EditRobotoRegular 
         android:id="@+id/editChatMsg" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="@drawable/linear_back" 
         android:hint="Type your message..." 
         android:imeOptions="actionSend" 
         android:inputType="textMultiLine|textCapSentences|text" 
         android:padding="@dimen/scale_5dp" 
         android:textColor="#5f6060" 
         android:textColorHint="#5f6060" 
         android:textSize="@dimen/text_14"/> 
       </LinearLayout> 

       <ImageButton 
        android:id="@+id/ivSend" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.3" 
        android:background="@android:color/transparent" 
        android:src="@drawable/ic_chat_icon"/> 

      </LinearLayout> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@color/grey_list" 
      android:gravity="center" 
      android:orientation="horizontal" 
      android:weightSum="2"> 

      <Button 
       android:id="@+id/buttonView" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:text="View" 
       android:textAllCaps="false" 
       android:textSize="@dimen/text_22"/> 

      <Button 
       android:id="@+id/buttonCancel" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:text="Cancel" 
       android:textAllCaps="false" 
       android:textSize="@dimen/text_22"/> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

而且不知道這是相關的,但這裏是我如何創建應答

String KEY_TEXT_REPLY = "key_text_reply"; 
String replyLabel = "Type here"; 
Intent intent = new Intent(context, NotificationActivity.class); 
intent.putExtra(Constants.REQUEST_ID, messageData.taskid); 
intent.putExtra(Constants.JUDE_ID, messageData.from); 
intent.putExtra(Constants.FROM, Constants.NOTIFICATION); 

PendingIntent pendingIntent = PendingIntent.getActivity(
       context, 
       0, 
       intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY) 
     .setLabel(replyLabel) 
     .build(); 

NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
       R.drawable.send_button, 
       "Reply", pendingIntent) 
       .addRemoteInput(remoteInput) 
       .build(); 

builder.addAction(replyAction); 

回答

2

要達到你想要什麼,你需要使用屬性android:launchMode與價值singleInstance

"singleTask"相同,只是系統不會將任何其他 活動啓動到保存該實例的任務中。該活動始終是其任務的唯一和唯一成員 。

您還應該添加android:excludeFromRecents="true"documentation解釋從最近使用的應用程序列表中排除您的活動:

無論是否通過本次活動發起的任務應該從列表中排除 最近使用的應用程序,總覽屏幕。 是,當此活動是新任務的根活動時,此 屬性確定該任務是否不應出現在最近的應用程序的列表中 。如果任務應該從列表中排除,則設置「true」; 如果應該包含它,則設置爲「false」。默認值是「false」。

總之,你需要改變你的AndroidManifest.xml這樣的:

<activity 
    android:name=".activity.NotificationActivity" 
    android:theme="@style/Theme.AppCompat.Light.Dialog.custom" 
    android:label="@string/title_activity_notification" 
    android:screenOrientation="portrait" 
    android:windowSoftInputMode="adjustResize|stateHidden" 
    android:launchMode="singleInstance" 
    android:excludeFromRecents="true" /> 
1

從通知開始專項活動描述here

對於這種情況,成立了PendingIntent以新鮮的 任務開始。不過,不需要創建退棧,因爲 開始的Activity不是應用程序活動流的一部分。 單擊後退仍然會將用戶帶到主屏幕。

特定活動不需要回堆棧,這樣你就不必在清單中界定其活動的層次結構,而不必調用addParentStack()建立一個後退堆棧。相反,通過調用getActivity()使用清單以建立活動任務選項,並創建PendingIntent

  1. 在你清單,添加以下屬性到

    元素活動

    android:name="activityclass" 
    

    活動的完全限定類名。

    android:taskAffinity="" 
    

    與您在代碼中設置FLAG_ACTIVITY_NEW_TASK標誌結合在一起,這確保了此活動不進入應用程序的默認 任務。任何具有應用程序的默認 親和力的現有任務都不受影響。

    android:excludeFromRecents="true" 
    

    排除從最近用過的新任務,使用戶可以不小心導航回到它。

代碼片斷

<activity 
    android:name=".YourActivity" 
    android:theme="@style/Theme.AppCompat.Light.Dialog.custom" 
... 
    android:launchMode="singleTask" 
    android:taskAffinity="" 
    android:excludeFromRecents="true"> 
</activity> 
  • 生成併發出通知(雖然你這樣做已經我建議只是檢查):

    • 創建一個啓動活動的Intent。

    • 通過調用setFlags()和標記FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_CLEAR_TASK,將活動設置爲在新的空任務中啓動。

    • 爲意圖設置您需要的其他選項。

    • 通過致電getActivity()從意圖創建一個PendingIntent。然後,您可以使用此PendingIntent作爲setContentIntent()的參數。

  • 代碼段。

    // Instantiate a Builder object. 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    // Creates an Intent for the Activity 
    Intent notifyIntent = 
         new Intent(this, ResultActivity.class); 
    // Sets the Activity to start in a new, empty task 
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
             | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    // Creates the PendingIntent 
    PendingIntent notifyPendingIntent = 
         PendingIntent.getActivity(
         this, 
         0, 
         notifyIntent, 
         PendingIntent.FLAG_UPDATE_CURRENT 
    ); 
    
    // Puts the PendingIntent into the notification builder 
    builder.setContentIntent(notifyPendingIntent); 
    // Notifications are issued by sending them to the 
    // NotificationManager system service. 
    NotificationManager mNotificationManager = 
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    // Builds an anonymous Notification object from the builder, and 
    // passes it to the NotificationManager 
    mNotificationManager.notify(id, builder.build()); 
    

    如果需要,還請檢查官方documentation

    希望這會幫助你。