Here below code is start alarm on particular time and stop alarm .
Here i create 2 buttons one is for start alarm after clicking on button 30 sec's and other stop alarm
button for stop.
Android UI Layout(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/startalarm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Alarm" />
<Button
android:id="@+id/stopalarm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Alarm" />
</LinearLayout>
Android Alarm Service (AlarmService.java)
package com.androidsurya.androidalarm;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class AlarmService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "AlaramService onCreate() called.",
Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "AlaramService onBind() called.",
Toast.LENGTH_SHORT).show();
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "AlaramService onDestroy() called.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
// Here you can add your own code to play any sound using media player
Toast.makeText(this, "AlaramService onStart() called.",
Toast.LENGTH_SHORT).show();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
// Here you can add your own code to media player which is all ready
// started early in onStart() method
Toast.makeText(this, "AlaramService onUnbind() called.",
Toast.LENGTH_SHORT).show();
return super.onUnbind(intent);
}
}
Android Activity(MainActivity.java)
package com.androidsurya.androidalarm;
import java.util.Calendar;
import com.androidsurya.androidalarm.R;
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.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
PendingIntent pendingIntent;
Button startAlarm, stopAlarm;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startAlarm = (Button) findViewById(R.id.startalarm);
stopAlarm = (Button) findViewById(R.id.stopalarm);
startAlarm.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getApplicationContext(),
AlarmService.class);
pendingIntent = PendingIntent.getService(
getApplicationContext(), 0, myIntent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(getApplicationContext(), "Start Alarm",
Toast.LENGTH_LONG).show();
}
});
stopAlarm.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Tell the user about what we did.
Toast.makeText(getApplicationContext(), "Stop Alarm!",
Toast.LENGTH_LONG).show();
}
});
}
}
Register Activity and Service in Android Manifest file
<activity
android:name="com.androidsurya.androidalarm.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".AlarmService" />
Output Screenshot:
Here i create 2 buttons one is for start alarm after clicking on button 30 sec's and other stop alarm
button for stop.
Android UI Layout(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/startalarm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Alarm" />
<Button
android:id="@+id/stopalarm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Alarm" />
</LinearLayout>
Android Alarm Service (AlarmService.java)
package com.androidsurya.androidalarm;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class AlarmService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "AlaramService onCreate() called.",
Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "AlaramService onBind() called.",
Toast.LENGTH_SHORT).show();
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "AlaramService onDestroy() called.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
// Here you can add your own code to play any sound using media player
Toast.makeText(this, "AlaramService onStart() called.",
Toast.LENGTH_SHORT).show();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
// Here you can add your own code to media player which is all ready
// started early in onStart() method
Toast.makeText(this, "AlaramService onUnbind() called.",
Toast.LENGTH_SHORT).show();
return super.onUnbind(intent);
}
}
Android Activity(MainActivity.java)
package com.androidsurya.androidalarm;
import java.util.Calendar;
import com.androidsurya.androidalarm.R;
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.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
PendingIntent pendingIntent;
Button startAlarm, stopAlarm;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startAlarm = (Button) findViewById(R.id.startalarm);
stopAlarm = (Button) findViewById(R.id.stopalarm);
startAlarm.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getApplicationContext(),
AlarmService.class);
pendingIntent = PendingIntent.getService(
getApplicationContext(), 0, myIntent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(getApplicationContext(), "Start Alarm",
Toast.LENGTH_LONG).show();
}
});
stopAlarm.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Tell the user about what we did.
Toast.makeText(getApplicationContext(), "Stop Alarm!",
Toast.LENGTH_LONG).show();
}
});
}
}
Register Activity and Service in Android Manifest file
<activity
android:name="com.androidsurya.androidalarm.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".AlarmService" />
Output Screenshot:
No comments:
Post a Comment