Main Activity:--
public class MainActivity extends Activity {
Button b1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
EditText ed1 = (EditText) findViewById(R.id.editText1);
int i = Integer.parseInt(ed1.getText().toString());
Intent intent = new Intent(getApplicationContext(), myBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(getApplicationContext(), "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();
}
});
}
}
BroadCast Receiver Activity :--
public class myBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context,"get up",Toast.LENGTH_LONG).show();
}
}
Activity main .xml:--
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFF">
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="46dp"
android:ems="15"
android:hint="number of seconds"
android:inputType="numberDecimal" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="36dp"
android:onClick="startAlert"
android:text="start Counter" />
</RelativeLayout>
Activity Manifest.xml:--
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreciever1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:enabled="true"
android:name=".myBroadcastReceiver"></receiver>
</application>
</manifest>
Comments
Post a Comment