Java Android如何每x小时或分钟执行一个方法
我正在尝试使用报警服务运行一个名为Java Android如何每x小时或分钟执行一个方法,java,android,Java,Android,我正在尝试使用报警服务运行一个名为Updater()的方法,为了进行测试,我希望它每1分钟更新一次,最终每2小时更新一次。如果切换按钮处于打开状态,则已调度任务在计时器上自行执行,当切换按钮处于关闭状态时,将停止计时器 这是到目前为止我的代码。有人能告诉我我做错了什么吗 public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, I
Updater()
的方法,为了进行测试,我希望它每1分钟更新一次,最终每2小时更新一次。如果切换按钮处于打开状态,则已调度任务在计时器上自行执行,当切换按钮处于关闭状态时,将停止计时器
这是到目前为止我的代码。有人能告诉我我做错了什么吗
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Updater();
}
}
public void autoUpdateClick(View view)
{
AlarmManager alarmManager=(AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
boolean on = ((ToggleButton) view).isChecked();
if (on)
{
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),30000,pendingIntent);
Toast.makeText(getBaseContext(),"Check-In will be done every 2 hours",Toast.LENGTH_SHORT).show();
}
else
{
alarmManager.cancel(pendingIntent);
Toast.makeText(getBaseContext(),"Manual Check-In enabled",Toast.LENGTH_SHORT).show();
}
}
正如您所提到的,我们将安排一个警报,以便在将来的特定时间执行一个方法 我们有两节课
你的问题到底是什么?方法不是每2分钟执行一次@LalMight-want-see似乎这已经被问到并回答了。谢谢@SimplyCraig5所以我需要将我的Updater()代码放在一个单独的类AlarmReceiver.java中,然后从那里调用它?有没有一种方法可以做到这一点,而不会唤醒手机并引起嗡嗡声,等等。?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.src"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- permission required to use Alarm Manager -->
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:icon="@drawable/ic_launcher"
android:label="Demo App" >
<activity
android:name=".MainActivity"
android:label="Demo App" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Register the Alarm Receiver -->
<receiver android:name=".AlarmReciever"/>
</application>
</manifest>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView1"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Alarm Manager Example"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_marginTop="25dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Schedule The Alarm"
android:onClick="scheduleAlarm"/>
</LinearLayout>
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void scheduleAlarm(View V)
{
// time at which alarm will be scheduled here alarm is scheduled at 1 day from current time,
// we fetch the current time in milliseconds and added 1 day time
// i.e. 24*60*60*1000= 86,400,000 milliseconds in a day
Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000;
// create an Intent and set the class which will execute when Alarm triggers, here we have
// given AlarmReciever in the Intent, the onRecieve() method of this class will execute when
// alarm triggers and
//we call the method inside onRecieve() method of Alarmreciever class
Intent intentAlarm = new Intent(this, AlarmReciever.class);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();
}
}
public class AlarmReciever extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//call the method here
}
}