Reputation: 1633
I have been struggling with the problem. I have a Broadcast receiver that is suppose to receive SMS' and it does that, but I want to use a method/function in my Service, the intent is to make it a foreground service eventually, that never shuts down. The Broadcastreceiver looks like this:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
//want to send msgs[] to SmsService
}
}
}
In the manifest for making it receive I have added:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BROADCAST_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS" />
<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:name=".SmsReceiver" >
<intent-filter>
<action android:name= "android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<service android:name="SmsService"></service>
</application>
Upvotes: 4
Views: 5072
Reputation: 7425
Intent myIntent = new Intent(context, SmsService.class);
myIntent.putExtra("msg", msgs);
context.startService(myIntent);
Upvotes: 5