警报框没有将应用程序带到前台

时间:2011-05-27 06:59:31

标签: android alertdialog

我正在开发一个Android应用程序,其中我添加了提醒功能。但是,如果应用程序在后台中运行,请说如果我在主屏幕并且应用程序在后台运行,则在提醒时警报框必须出现在屏幕上而不将应用程序带到前景 。这可能吗?

4 个答案:

答案 0 :(得分:0)

警报对话框是根据您的活动的上下文创建的,因此您的活动(应用程序)将位于前台。

为什么不为此发送状态栏通知?

答案 1 :(得分:0)

使用服务来处理提醒事项并启动提醒消息警告框,它基本上是一个活动或对话框。              但是对于锁定屏幕,它可能会也可能不会显示警报,因为您应该在Activity的create方法中使用以下代码

// add flag to show activity window if screen is locked.
        Window window = this.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

上面的代码应该在“super.onCreate(pSavedInstanceState);”

之后

答案 2 :(得分:0)

你应该把提醒代码放到像这样的服务中

public class MyService extends Service {

private Timer timer = new Timer();
private static final long UPDATE_INTERVAL = 3600000;
public MediaPlayer mp;
public String ns =  Context.NOTIFICATION_SERVICE; 
public NotificationManager mNotificationManager;
public int icon = R.drawable.calendar;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    mp = MediaPlayer.create(this, R.raw.mail120);
    mNotificationManager  = (NotificationManager) getSystemService(ns);
    //Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {
    super.onDestroy();
    timer.cancel();
    //Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

}

@Override
public void onStart(Intent intent, int startId) {

    super.onStart(intent, startId);
    _startService(this);


}

public void _startService(final Context context) {
    notified = new ArrayList<String>();
    notified_ergs = "";
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {

             //do some stuff
        }
    },0,UPDATE_INTERVAL);    
}
 }

并注册一个Broadcastreceiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyStartupIntentReceiver extends BroadcastReceiver{
 @Override
 public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("[packagename].MyService");
context.startService(serviceIntent);

}
}

告诉reciver在Boot上启动(在android Manifest中

<receiver android:name=".MyStartupIntentReceiver">
        <intent-filter>
            <action
                android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </receiver>

现在您可以播放声音,向通知栏发送消息或在不启动应用程序的情况下举报某些消息

答案 3 :(得分:0)

将活动启动模式设置为单个实例在清单中会很好〜