本文介绍了Android的计划任务要在特定时间每天执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在每天特定的时间来改变我的铃声音量。我用了一个日历指定的时间,我试图用alarmManager执行它。下面是我。 (我是noobie宽容我)
包com.example.ringervolume.app;进口android.app.Activity;
进口android.app.AlarmManager;
进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.media.AudioManager;
进口android.os.Bundle;
进口android.support.v7.app.ActionBarActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.TextView;进口的java.util.Calendar;
公共类MainActivity扩展ActionBarActivity {
私人AudioManager音频;
私人的PendingIntent pendingIntentam;
私人的PendingIntent pentdingIntentpm;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); TextView的textViewTime =(的TextView)findViewById(R.id.textTime);
音频=(AudioManager)getSystemService(Context.AUDIO_SERVICE); //铃声音量的上午。
日历calendaram = Calendar.getInstance(); calendaram.set(Calendar.HOUR_OF_DAY,21);
calendaram.set(Calendar.MINUTE,32);
calendaram.set(Calendar.SECOND,0);
calendaram.set(Calendar.AM_PM,Calendar.PM); 意图myIntent =新意图(MainActivity.this,MyReceiver.class);
pendingIntentam = PendingIntent.getBroadcast(MainActivity.this,0,myIntent,0);
AlarmManager alarmManageram =(AlarmManager)getSystemService(ALARM_SERVICE);
alarmManageram.set(AlarmManager.RTC,calendaram.getTimeInMillis(),pendingIntentam);
} 公共类MyReceiver扩展广播接收器{ @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
意图scheduledIntent =新意图(背景下,myscheduleactivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent); }
} 公共类myscheduleactivity延伸活动{ @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){ //充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}}
解决方案
要火灾报警每天在21:32
私人AlarmManager alarmMgr;
私人的PendingIntent alarmIntent;
...
alarmMgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
意向意图=新意图(背景下,AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(上下文,0,意图,0);//设置闹钟在21:32 PM开始
台历挂历= Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis的());
calendar.set(Calendar.HOUR_OF_DAY,21);
calendar.set(Calendar.MINUTE,32);// setRepeating(),您可以指定precise自定义的时间间隔 - 在这种情况下,
// 1天
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,alarmIntent);
在这种情况下, AlarmReceiver
是广播接收器,它已经有一个背景下,这样你就可以振铃模式直接设置为从广播接收沉默,而无需启动一个活动。
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
AudioManager上午=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
请一定要添加这些权限:
<使用许可权的android:NAME =android.permission.WRITE_SETTINGS>< /使用许可权>
<使用许可权的android:NAME =android.permission.CHANGE_CONFIGURATION>< /使用许可权>
<使用许可权的android:NAME =android.permission.MODIFY_AUDIO_SETTINGS>< /使用许可权>
I am wanting to change my ringtone volume at specific time each day. I used a calendar to specify the time, and I am attempting to use alarmManager to execute it. Here is what I have. (I am a noobie go easy on me)
package com.example.ringervolume.app;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends ActionBarActivity {
private AudioManager audio;
private PendingIntent pendingIntentam;
private PendingIntent pentdingIntentpm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewTime = (TextView) findViewById (R.id.textTime);
audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
//ringer volume for the am.
Calendar calendaram = Calendar.getInstance();
calendaram.set(Calendar.HOUR_OF_DAY, 21);
calendaram.set(Calendar.MINUTE, 32);
calendaram.set(Calendar.SECOND, 0);
calendaram.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntentam = PendingIntent.getBroadcast(MainActivity.this, 0,myIntent,0);
AlarmManager alarmManageram = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManageram.set(AlarmManager.RTC, calendaram.getTimeInMillis(), pendingIntentam);
}
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent scheduledIntent = new Intent(context, myscheduleactivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
}
public class myscheduleactivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
解决方案
To fire alarm everyday, at 21:32
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 21:32 PM
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 32);
// setRepeating() lets you specify a precise custom interval--in this case,
// 1 day
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
In this case, AlarmReceiver
is the Broadcast Receiver and it already has a context, so you can directly set the ringer mode to silent from the Broadcast Receiver without starting an activity.
@Override
public void onReceive(Context context, Intent intent) {
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
Please be sure to add these permissions :
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" ></uses-permission>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>
这篇关于Android的计划任务要在特定时间每天执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!