gpt4 book ai didi

android安排任务每天在特定时间执行

转载 作者:IT老高 更新时间:2023-10-28 23:21:31 25 4
gpt4 key购买 nike

我想在每天的特定时间更改我的铃声音量。我使用日历来指定时间,并且我正在尝试使用 alarmManager 来执行它,但它不会将音量设置为在我指定的时间振动。这就是我所拥有的。 (我是个菜鸟,放轻松)。

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);
}

}

最佳答案

每天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);

在这种情况下,AlarmReceiver 是广播接收器,它已经有一个上下文,因此您可以直接从广播接收器将铃声模式设置为静音,而无需启动 Activity 。

@Override
public void onReceive(Context context, Intent intent) {
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}

请务必添加这些权限:

<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安排任务每天在特定时间执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24196890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com