土司可以正常显示 但没有声音 且添加的正在播放的日志没有正常输出
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SMSMusic"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>
package com.example.smsmusic;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button btnStop;
private boolean isCast; //是否为广播激活
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{"android.permission.RECEIVE_SMS"},1);
}
btnStop=findViewById(R.id.btnStop);
Intent intent = getIntent(); //获取广播意图对象
isCast = intent.getBooleanExtra("iscast", false); //默认值为false
btnStop.setEnabled(isCast); //设置停止按钮可用和单击监听
if(isCast) Toast.makeText(this, "正在播放音乐...", Toast.LENGTH_SHORT).show();
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
//显式服务调用意图(非绑定式)
Intent intent=new Intent(MainActivity.this,MyAudioService.class);
//在Activity组件里,停止音乐播放服务
stopService(intent);
finish(); //销毁本活动
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case 1:
if(grantResults[0]!=PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "未授权,无法实现预定的功能!", Toast.LENGTH_SHORT).show();
finish();
}else{
Toast.makeText(this, "请发一条短信验证...", Toast.LENGTH_SHORT).show();
}
}
}
}
package com.example.smsmusic;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import java.io.IOException;
public class MyAudioService extends Service {
private final String TAG="music";
MediaPlayer mediaPlayer;
public MyAudioService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//throw new UnsupportedOperationException("Not yet implemented");
return null;
}
public void onCreate() {
// mediaPlayer = MediaPlayer.create(this,R.raw.start);
// mediaPlayer.start();
// Log.i(TAG,"正在播放");
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getResources().openRawResourceFd(R.raw.start).getFileDescriptor());
mediaPlayer.prepare();
mediaPlayer.start();
Log.i(TAG, "正在播放");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
mediaPlayer.stop();
}
}
package com.example.smsmusic;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
Intent serviceIntent = new Intent(context, MyAudioService.class);
//在广播组件里,通过上下文对象启动音乐播放服务组件
context.startService(serviceIntent);
//新建调用Activity组件的意图
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.putExtra("iscast", true); //携带数据
//新建栈用来存放被启动的Activity(当已经存在时,只做移动处理)
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//在广播组件里,通过上下文对象启动Activity组件
context.startActivity(activityIntent);
}
}
土司可以正常显示 但没有声音且添加的日志Log.i(TAG, "正在播放");没有正常输出