Monday, 29 April 2013
BroadCast in android
Ref:
http://www.javacodegeeks.com/2012/08/android-broadcast-receiver-enable-and.html
activity_brord_casting.xml
----------------------------
<LinearLayout 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:orientation='vertical'>
<Button
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:padding='10dip'
android:text='@string/start_repeating_alarm'
android:onClick='startRepeatingAlarm'
tools:context='.EnableDisableBroadcastReceiver' />
<Button
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:padding='10dip'
android:text='@string/cancel_alarm'
android:onClick='cancelAlarm'
tools:context='.EnableDisableBroadcastReceiver' />
<Button
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:padding='10dip'
android:text='@string/enable_broadcast_receiver'
android:onClick='enableBroadcastReceiver'
tools:context='.EnableDisableBroadcastReceiver' />
<Button
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:padding='10dip'
android:text='@string/disable_broadcast_receiver'
android:onClick='disableBroadcastReceiver'
tools:context='.EnableDisableBroadcastReceiver' />
</LinearLayout>
BrordCastingActivity.Java
-----------------------------
package com.example.broadcastingexample;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.Toast;
public class BrordCastingActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brord_casting);
}
/**
* This method gets called when 'Start Repeating Alarm' button is pressed.
* It sets the repeating alarm whose periodicity is 3 seconds.
* @param view
*/
public void startRepeatingAlarm(View view)
{
AlarmManager am=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
//After after 2 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 4 , pi);
Toast.makeText(this, "Started Repeating Alarm", Toast.LENGTH_SHORT).show();
}
/**
* This method gets called when 'cancel Alarm' button is pressed.
* This method cancels the previously set repeating alarm.
* @param view
*/
public void cancelAlarm(View view)
{
Intent intent = new Intent(this, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
Toast.makeText(this, "Cancelled alarm", Toast.LENGTH_SHORT).show();
}
/**
* This method enables the Broadcast receiver registered in the AndroidManifest file.
* @param view
*/
public void enableBroadcastReceiver(View view){
ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
}
/**
* This method disables the Broadcast receiver registered in the AndroidManifest file.
* @param view
*/
public void disableBroadcastReceiver(View view){
ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
}
}
AlarmManagerBroadcastReceiver.Java
------------------------------------------
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
final private String tag="tag";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(tag, "msg");
//You can do the processing here update the widget/remote views.
StringBuilder msgStr = new StringBuilder();
Log.i(tag, "msg2");
//Format time.
Format formatter = new SimpleDateFormat("hh:mm:ss a");
Log.i(tag, "msg3");
msgStr.append(formatter.format(new Date()));
Log.i(tag, "msg4");
Toast.makeText(context, msgStr, Toast.LENGTH_SHORT).show();
Log.i(tag, "msg5");
}
}
androidmanitest
--------------------
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastingexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcastingexample.BrordCastingActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Broadcast receiver -->
<receiver android:name='com.example.broadcastingexample.AlarmManagerBroadcastReceiver' />
</application>
</manifest>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment