Android的活动涉及到前台经理报警后,涉及到、前台、经理、Android

2023-09-12 05:59:30 作者:隐身守候*

我的应用程序,这使得事件的报警管理,并在特定的时间的调用。 code看起来像这样

I have application, which makes event in alarm manager, and at specific time its called. Code looks like this

Intent intent = new Intent(this, AlarmActivity.class);
pendingIntent = PendingIntent.getActivity(this,req_code, intent, PendingIntent.FLAG_CANCEL_CURRENT);    
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY*7,
                    pendingIntent);

意图把这种活动。

Intent calls this activity.

public class AlarmActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void onStart(){
        super.onStart();
        //Change ringer mode
        //Add notification in status bar
        //Other booring stuff here...
        Toast.makeText(this,"Finishing",2000).show();
        finish();
    }
}

在booring stuffthere都是code应在后台运行(其他城市ringermode)

in booring stuffthere are is code which should run in background (change ringermode)

一切工作对我来说,除了一件事。每当警报管理器调用我的活动 - 应用程序涉及到前台。 当它应该只改变振铃模式,背景,并且在状态栏添加通知。

Everything works for me, except one thing. Whenever the alarm manager calls my activity - the application comes to foreground. When it only should change ringer mode in background, and add notification in status bar.

任何方式不允许应用程序来前景?

Any way to not allow application come to foreground?

推荐答案

您应该做的这一切在的 BroadcastReceiver的。没有用户界面,并有一个上下文变量传递到接收器的的onReceive()方法,它可以让你基本上做什么活动呢,不需要实际的用户界面。这意味着,你可以设置铃声,显示状态栏通知等。 你的BroadcastReceiver类应该是这个样子:

You should do all of this in a BroadCastReceiver. There is no UI, and there is a Context variable passed on to the Receiver's onReceive() method which allows you to basically do anything the Activity does, without having an actual UI. This means that you can set the ringer, show the status bar notification, etc. Your BroadcastReceiver class should look something like:

public class AlarmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    //Change ringer mode
    //Add notification in status bar
    //Other boring stuff here...
    Toast.makeText(context,"Finishing",2000).show();
    }
}

请注意,对于你的吐司,命名变量上下文被使用。

Note that for your Toast, the variable named context is used.

和您的 AlarmManager code应该是这个样子:

And your AlarmManager code should look something like this:

Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,req_code, intent, PendingIntent.FLAG_CANCEL_CURRENT);    
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY*7,
                    pendingIntent);

您的清单应该有这样的:

Your manifest should have this:

 <receiver android:name=".AlarmBroadcastReceiver" >
        </receiver>