Hello Friend,
Today I am going to share code for "Create Alarm Manager in Android". And Notification Manager with sound and vibration.
1)MainActivity-
package com.manish.alarmmanager;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
*
* @author manish
*
*/
public class MainActivity extends Activity implements OnClickListener{
Button btnStartAlarm,btnStopAlarm;
Context context;
static PendingIntent pendingIntent;
static AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=MainActivity.this;
Intent intentsOpen = new Intent(this, AlarmReceiver.class);
intentsOpen.setAction("com.manish.alarm.ACTION");
pendingIntent = PendingIntent.getBroadcast(this,111, intentsOpen, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
btnStartAlarm=(Button)findViewById(R.id.button1);
btnStopAlarm=(Button)findViewById(R.id.button2);
btnStartAlarm.setOnClickListener(this);
btnStopAlarm.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==btnStartAlarm){
fireAlarm();
}
if(v==btnStopAlarm){
stopAlarm();
}
}
public void fireAlarm() {
/**
* call broadcost reciver
*/
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10000, pendingIntent);
}
public void stopAlarm(){
alarmManager.cancel(pendingIntent);
}
}
Today I am going to share code for "Create Alarm Manager in Android". And Notification Manager with sound and vibration.
EDIT: stop was not working
1)MainActivity-
package com.manish.alarmmanager;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
*
* @author manish
*
*/
public class MainActivity extends Activity implements OnClickListener{
Button btnStartAlarm,btnStopAlarm;
Context context;
static PendingIntent pendingIntent;
static AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=MainActivity.this;
Intent intentsOpen = new Intent(this, AlarmReceiver.class);
intentsOpen.setAction("com.manish.alarm.ACTION");
pendingIntent = PendingIntent.getBroadcast(this,111, intentsOpen, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
btnStartAlarm=(Button)findViewById(R.id.button1);
btnStopAlarm=(Button)findViewById(R.id.button2);
btnStartAlarm.setOnClickListener(this);
btnStopAlarm.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==btnStartAlarm){
fireAlarm();
}
if(v==btnStopAlarm){
stopAlarm();
}
}
public void fireAlarm() {
/**
* call broadcost reciver
*/
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10000, pendingIntent);
}
public void stopAlarm(){
alarmManager.cancel(pendingIntent);
}
}
3)OutPut.java
package com.manish.alarmmanager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class OutPut extends Activity {
TextView textmessage;
String stringValue;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_output);
textmessage = (TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
stringValue = intent.getStringExtra("content");
textmessage.setText(stringValue);
System.out.println(stringValue);
}
}
4)Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manish.alarmmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.manish.alarmmanager.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>
<activity android:name="com.manish.alarmmanager.OutPut" />
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="com.manish.alarm.ACTION" />
</intent-filter>
</receiver>
</application>
Thanks!