Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果我的应用程序已通过按home(主页)按钮最小化,则我的通知服务将启动主要活动_Android - Fatal编程技术网

Android 如果我的应用程序已通过按home(主页)按钮最小化,则我的通知服务将启动主要活动

Android 如果我的应用程序已通过按home(主页)按钮最小化,则我的通知服务将启动主要活动,android,Android,我的应用程序有一个通知服务。问题是当我的应用程序被最小化(按home按钮),通知服务发出通知,我的主要活动将自行启动。它真的很烦人,我需要修复它 任何一个有可能的解决方案 我尝试使用android:launchMode=singleInstance作为解决方案@,但这个解决方案的问题是,如果我尝试使用长按home按钮重新启动我的应用程序,该按钮会给出最近使用的应用程序列表,我的应用程序永远不会运行 为了重新运行它,然后我必须去我的应用程序的图标在菜单中,并从那里打开它 有没有办法解决这两个问题

我的应用程序有一个通知服务。问题是当我的应用程序被最小化(按home按钮),通知服务发出通知,我的主要活动将自行启动。它真的很烦人,我需要修复它

任何一个有可能的解决方案

我尝试使用android:launchMode=singleInstance作为解决方案@,但这个解决方案的问题是,如果我尝试使用长按home按钮重新启动我的应用程序,该按钮会给出最近使用的应用程序列表,我的应用程序永远不会运行

为了重新运行它,然后我必须去我的应用程序的图标在菜单中,并从那里打开它

有没有办法解决这两个问题

生成通知的活动:

包org.example.com

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class ParentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_copy);

    NotificationManager newNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    final Notification notificationDetails = new Notification(R.drawable.icon,"Downloads available!!",System.currentTimeMillis());
    notificationDetails.flags = Notification.FLAG_AUTO_CANCEL;

    Intent newIntent = new Intent(this,ParentActivity.class);
    newIntent.putExtra("IS_FOREGROUND",1);
    //newIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent newPendingIntent = PendingIntent.getActivity(this, 0, newIntent, 0);

    CharSequence contentTitle = "Downloads Available!!";
    CharSequence contentText  = "Some new Downloads are available, click to enable.";
    int notificationId = 3;

    notificationDetails.setLatestEventInfo(this, contentTitle, contentText, newPendingIntent);
    newNotificationManager.notify(notificationId, notificationDetails);

    finish();

}
}

我在你的代码中看到你正在进行一项活动,这就是我认为你的应用程序走到前台的原因。您的服务是否启动此活动?
如果是触发通知的服务,为什么不在该服务中创建通知?然后你的应用程序将留在后台。

你无法控制home按钮的行为……当通知被触发时,你的应用程序应该怎么做?@vineetska:我不想控制home按钮的行为,我想要的只是通知服务不启动我的最小化应用程序的一种方式。@Zoleas:服务会定期发出XYZ更新的通知,但当用户正在使用该应用程序并将该应用程序最小化一段时间后,比如短信等,通知服务触发一个通知,并带来Intent i=newintentgetapplicationcontext,TargetActivity.class中提到的通知的目标活动;到前台。只有当应用程序最小化时才会发生这种情况。但当应用程序未运行时,它运行良好,即它仅正确显示通知。您可以发布创建通知的代码吗?我在该活动中还有其他内容,需要将其设置为活动。。那么,还有什么其他选择呢?是的,但我的意思是:当您的服务触发通知时,是否要启动此活动?如果启动此活动,屏幕上将显示一些内容。如果您只想执行后台任务,如在状态栏中创建通知,请改用服务。如果用户正在使用该应用,“我的活动”将显示一个警报对话框,如果用户未使用该应用,则会显示有关更新的通知。此警报对话框包含一些确认消息和所有。。。我的活动只有在按下home(主页)按钮将应用程序最小化时才会启动。。。当应用程序未运行时,服务仅触发通知且不启动活动。。。从3天起我就一直在做这项工作……你可以考虑改变你的申请行为。在状态栏中只显示通知而不启动活动怎么样?当用户点击通知时,您仍然可以启动活动并显示弹出窗口。感谢Zoleas,我在服务中使用了通知,对于其他事项,有条件检查会启动活动[如果需要]