我有一个COMPLETED ON BOOT服务,如果我收到短信,音乐会立即开始。
我做错了吗?
public class MyService extends Service {
final MediaPlayer mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.son1);
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
AudioManager mAudioManager = (AudioManager)getApplication().getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
mPlayer.start();
}
}
};
}
<service android:name=".MyService"/>
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
答案 0 :(得分:0)
我建议您在AndroidManifest.xml中使用android:priority="999"
,如下所示,
<receiver
android:name="com.application.reciever.SMSReceiver"
class="com.application.reciever.SMSReceiver" android:exported="true">
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
这样您的应用程序就可以获得SMS Receive事件的优先权。
public class SMSReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context mContext, Intent intent)
{
// Your code to play music
}
}
答案 1 :(得分:0)
您可能会错过接收SMS_RECEIVED
广播的权限。尝试添加到您的清单:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>