Updater()
나는 알람 서비스로 호출되는 메소드를 실행하려고 시도하고 테스트를 위해 1 분마다 그리고 결국 2 시간마다 업데이트하기를 원합니다. Sceduled 작업은 토글 버튼이 켜져 있으면 타이머에서 실행되고 토글 버튼이 꺼져 있으면 타이머를 중지합니다. 이것이 지금까지 내 코드입니다. 누군가 내가 뭘 잘못하고 있는지 보여줄 수 있습니까?
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();
}
}
앞서 언급했듯이 향후 특정 시간에 메서드를 실행하도록 알람을 예약 할 것입니다.
우리는 두 가지 수업을
AlarmReciever 클래스는 BroadcastReceiver를 확장하고 onRecieve () 메서드를 재정의합니다. onReceive () 내에서 전화를 진동하거나 전화를 울리는 활동을 시작할 수있는 것처럼 필요에 따라 활동 또는 서비스를 시작할 수 있습니다.
권한 필요 애플리케이션에서 AlarmManger를 사용하려면 권한이 필요하므로 매니페스트 파일에서 권한을 선언하는 것을 잊지 마십시오.
<?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
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다