开发者

How to create BroadcastReceiver without Activity

开发者 https://www.devze.com 2023-02-01 04:30 出处:网络
I would like to create a application that would respond to receiving SMS messages and display a dialog. How can I register the receiver in the manifest without defining within an activity?

I would like to create a application that would respond to receiving SMS messages and display a dialog. How can I register the receiver in the manifest without defining within an activity?

I tried to keep the receiver/intent-filter tags in the manifest out of the activity tag but the emulator will not install the apk since there is no launch activity. Keeping the BroadcastReceiver as the main activity results in an "Unable to instantiate activity" error in the Logcat.

Any help?

Thanks, Sunny

Receiver class

public class SMSReceiver extends BroadcastReceiver {

 // onCreat is invoked when an sms message is received.
 // Message is attached to Intent via Bundle, stored in an Object
 // array in the PDU format.
 public void onReceive(Context context, Intent intent) {
  // get the SMS message passed in from Bundle
  Bundle bundle = intent.getExtras();        
    String bodyText = "";            
    String from = "";
    if (bundle != null) {
        //Retrieve sms message within Object array
        Object[] pdus = (Object[]) bundle.get("pdus");
        SmsMessage[] msgs = new SmsMessage[pdus.length];            

        for (int i=0; i < msgs.length; i++)
         msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

        for (SmsMessage message: msgs) {
         bodyText = message.getMessageBody();
         from = "Message from " + message.getOriginatingAddress() + ": ";
        }
        // Display message in pop up
        Toast.makeText(context, from + bodyText, Toast.LENGTH_SHORT).show();
    }                  
 }
}

Manifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android开发者_JAVA技巧" package="home.splttingatms.SMSReceiver" android:versionCode="1" android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SMSReceiver"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

  <receiver android:name=".SMSReceiver">
   <intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
   </intent-filter>
  </receiver>
 </application>

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

</manifest> 


One way to hide the Activity is by using the following theme in the Android Manifest File of your application:

<activity
    android:name=".SomeActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

NOTE: Using this does make your app not show up when launched, but it still does not get rid of the launcher icon. And if you remove the icon you wouldn't be able to launch the app. (Figure your way around that one!)


What you are trying to do is wrong for at least the following reasons...

  1. MAIN/LAUNCHER only apply to activities and as you don't have a class which extends Activity in your code, this is what is causing the error.
  2. Although there's nothing wrong with an 'app' which implements just a BroadcastReceiver or Service, it is good practice to let the user know that things have been initialized correctly. Even if you could list a receiver/service in 'All apps', if they select it and see nothing happens they're not going to be happy - users like to see some feedback.

In short, create a simple Activity which will appear in 'All apps' and has the MAIN/LAUNCHER intent settings and when it starts just have it create a dialog saying something like "Hi, welcome to ..." or some other feedback to the user letting them know that things have started correctly. Have an 'OK' button on the dialog which when pressed, calls the Activity's finish() method leaving the receiver in place.


from sendBroadcast() documentation:

No results are propagated from receivers and receivers can not abort the broadcast. If you want to allow receivers to propagate results or abort the broadcast, you must send an ordered broadcast using sendOrderedBroadcast(Intent, String).

use sendOrderedBroadCast() instead

0

精彩评论

暂无评论...
验证码 换一张
取 消