package com.example.alarmmanagerexample;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AlarmManagerExample extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_manager_example);
Button buttonStart=(Button)findViewById(R.id.buttonStart);
Button buttonStop=(Button)findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startCall();
}
private void startCall() {
Log.d("****", "Start");
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(AlarmManagerExample.this, MyService.class);
PendingIntent pintent = PendingIntent.getService(AlarmManagerExample.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pintent);
Log.d("****", "Start");
}
});
//stop service
buttonStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(getBaseContext(), MyService.class));
}
});
}
}
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AlarmManagerExample extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_manager_example);
Button buttonStart=(Button)findViewById(R.id.buttonStart);
Button buttonStop=(Button)findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startCall();
}
private void startCall() {
Log.d("****", "Start");
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(AlarmManagerExample.this, MyService.class);
PendingIntent pintent = PendingIntent.getService(AlarmManagerExample.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pintent);
Log.d("****", "Start");
}
});
//stop service
buttonStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(getBaseContext(), MyService.class));
}
});
}
}
No comments:
Post a Comment