Alarm service : Service « Network « Android






Alarm service

  

//http://moazzam-khan.com/blog/?p=157


package app.test;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
 class AlarmReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent arg1) {
    context.startService(new Intent(context, AlarmReceiver.class));
  }
}
public class Test extends Activity implements OnClickListener {
  private static Test appRef = null;
  private Button b_call_service, b_exit_service;
  boolean k = false;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    appRef = this;
    setContentView(R.layout.main);
    b_call_service = (Button) findViewById(R.id.call_alarm_service);
    b_call_service.setOnClickListener(this);
    b_exit_service = (Button) findViewById(R.id.exit);
    b_exit_service.setOnClickListener(this);
  }

  public static Test getApp() {
    return appRef;
  }

  public void btEvent(String data) {
    setTitle(data);
  }

  public void onClick(View arg0) {
    if (arg0 == b_call_service) {
      setTitle("Waiting... Alarm=5");
      Intent intent = new Intent(Test.this, AlarmReceiver.class);
      PendingIntent p_intent = PendingIntent.getBroadcast(
          Test.this, 0, intent, 0);
      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(System.currentTimeMillis());
      calendar.add(Calendar.SECOND, 5);
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
          p_intent);
    }

    if (arg0 == b_exit_service) {
      Intent intent = new Intent(Test.this, AlarmReceiver.class);
      PendingIntent p_intent = PendingIntent.getBroadcast(
          Test.this, 0, intent, 0);
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      am.cancel(p_intent);
      finish();

    }
  }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
    <Button
    android:id="@+id/call_alarm_service"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="call_alarm_service"   
    />
    
   <Button
    android:id="@+id/exit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Exit"   
    />
    
</LinearLayout>

   
    
  








Related examples in the same category

1.Map Service
2.Widget Service
3.Restful web service task
4.Search with Restful service
5.Post Restful service
6.Tracker Service
7.Weather web service
8.Access a Web service using GET
9.Get Running Services Info
10.Local service demo
11.extends Service
12.Service structure
13.Clock Back Service
14.Voice Recognition Service
15.is Connected by Context.CONNECTIVITY_SERVICE
16.BeatService extends Service and Thread
17.TimeoutService (beta). Here you can register a timeout.
18.Timeout Service
19.Screenshot Service