我打开一个名为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,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);
解决方法
android:launchMode
的值为singleInstance:
Same as
"singleTask"
,except that the system doesn’t launch any other
activities into the task holding the instance. The activity is always
the single and only member of its task.
您还应该添加android:excludeFromRecents =“true”,以将您的活动从最近使用的应用程序列表中排除,如documentation所述:
Whether or not the task initiated by this activity should be excluded
from the list of recently used applications,the overview screen. That
is,when this activity is the root activity of a new task,this
attribute determines whether the task should not appear in the list of
recent apps. Set “true” if the task should be excluded from the list;
set “false” if it should be included. The default value is “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" />