Sunday 15 April 2012

java - Create an automatic launch-able Broadcast Receiver in Android -



java - Create an automatic launch-able Broadcast Receiver in Android -

problem:

i willing create application starts background process , whenever new message comes device should log file or display toast message.

i have read lot of blogs , tried follow steps mentioned. but, maintain on sending messages on device , nil displayed not in device log. want run on devices froyo lollipop. so, not willing utilize new telephony api supports api 19 , later versions.

manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.abc.test" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="15" /> <uses-permission android:name="android.permission.receive_sms"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <receiver android:name=".smshandler"> <intent-filter> <action android:name="android.provider.telephony.sms_received" /> </intent-filter> </receiver> </application> </manifest>

source file

package com.abc.test; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.widget.toast; public class smshandler extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // todo auto-generated method stub toast toast = toast.maketext(context, "message initiated", toast.length_long); toast.show(); if (intent.getaction() .equals("android.provider.telephony.sms_received")) { toast = toast.maketext(context, "message received", toast.length_long); toast.show(); } } }

environment:

ide:

android studio

min sdk version:

8

tested on:

ics device (sony xperia u) kit-kat (moto g)

you need add together activity, run activity, before broadcastreceiver work.

more accurately, needs utilize explicit intent before app moved out of stopped state , allow manifest-registered broadcastreceivers work. simplest way have launcher activity, , run activity launcher.

to larn more, see "launch controls on stopped applications" in the android 3.1 release notes, along this blog post.

java android android-intent broadcastreceiver android-broadcast

No comments:

Post a Comment