Created
March 2, 2014 19:38
-
-
Save csdear/993697747dad930d68c7 to your computer and use it in GitHub Desktop.
Broadcast Receiver : Simple Statice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Broadcaster | |
package course.examples.BroadcastReceiver.singleBroadcastStaticRegistration; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
public class SimpleBroadcast extends Activity { | |
//1. Create custom intent string | |
private static final String CUSTOM_INTENT = "course.examples.BroadcastReceiver.show_toast"; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
Button button = (Button) findViewById(R.id.button); | |
button.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
//2. Send the Intent, but only to those with permission VIBRATE | |
sendBroadcast(new Intent(CUSTOM_INTENT), | |
android.Manifest.permission.VIBRATE); | |
} | |
}); | |
} | |
} | |
====================================================== | |
//Receiver.java | |
package course.examples.BroadcastReceiver.singleBroadcastStaticRegistration; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Vibrator; | |
import android.util.Log; | |
import android.widget.Toast; | |
//1. extend BroadcastReceiver | |
public class Receiver extends BroadcastReceiver { | |
private final String TAG = "Receiver"; | |
//2. Call onReceive Method To Stuff' | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.i(TAG, "INTENT RECEIVED"); | |
Vibrator v = (Vibrator) context | |
.getSystemService(Context.VIBRATOR_SERVICE); | |
v.vibrate(500); | |
Toast.makeText(context, "INTENT RECEIVED by Receiver", Toast.LENGTH_LONG).show(); | |
} | |
} | |
========================================================= | |
============================================================== | |
//AndroidManifest.xml | |
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="course.examples.BroadcastReceiver.singleBroadcastStaticRegistration" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="10" | |
android:targetSdkVersion="18" /> | |
<uses-permission android:name="android.permission.VIBRATE" > | |
</uses-permission> | |
<application | |
android:allowBackup="false" | |
android:icon="@drawable/icon" | |
android:label="@string/app_name" > | |
<activity | |
android:name=".SimpleBroadcast" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
//1. Create a receiver & intent-filters | |
<receiver | |
android:name=".Receiver" | |
android:exported="false" > | |
<intent-filter> | |
<action android:name="course.examples.BroadcastReceiver.show_toast" > | |
</action> | |
</intent-filter> | |
</receiver> | |
</application> | |
</manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment