Wednesday, June 26, 2013

Alarm demo

Today I am going to share the knowledge about Alarm coding in java. There is class in Android API that manage the Alarm that is AlarmManager .And need to registered the broadcast Register, that one time it automatically trigger.

Main.xml

?
1
2
3
4
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
  <button android:id="@+id/start" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Start Alarm">
  </button><button android:id="@+id/stop" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Cancel Alarm">
</button></linearlayout>

AlarmActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package app.test;
  
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.view.View;
import android.widget.Toast;
  
public class AlarmActivity extends Activity implements View.OnClickListener {
      
    private PendingIntent mAlarmIntent;
      
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
        Intent launchIntent = new Intent(this, AlarmReceiver.class);
        mAlarmIntent = PendingIntent.getBroadcast(this, 0, launchIntent, 0);
    }
  
    @Override
    public void onClick(View v) {
        AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        long interval = 5*1000; //5 seconds
          
        switch(v.getId()) {
        case R.id.start:
            Toast.makeText(this, "Scheduled", Toast.LENGTH_SHORT).show();
            manager.setRepeating(AlarmManager.ELAPSED_REALTIME,
                    SystemClock.elapsedRealtime()+interval,
                    interval,
                    mAlarmIntent);
            break;
        case R.id.stop:
            Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show();
            manager.cancel(mAlarmIntent);
            break;
        default:
            break;
        }
    }
      
    private long nextStartTime() {
        long oneDay = 24*3600*1000; //24 hours
        //Set the time to 09:00:00
        Calendar startTime = Calendar.getInstance();
        startTime.set(Calendar.HOUR_OF_DAY, 9);
        startTime.set(Calendar.MINUTE, 0);
        startTime.set(Calendar.SECOND, 0);
          
        Calendar now = Calendar.getInstance();
        if(now.before(startTime)) {
            return startTime.getTimeInMillis();
        } else {
            startTime.add(Calendar.DATE, 1);
            return startTime.getTimeInMillis();
        }
    }
}

AlarmReceiver.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package app.test;
  
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
  
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
  
public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Calendar now = Calendar.getInstance();
        DateFormat formatter = SimpleDateFormat.getTimeInstance();
        Toast.makeText(context, formatter.format(now.getTime()), Toast.LENGTH_SHORT).show();
    }
}