Witam!
Moja wiedza na temat programowania jest mała, dlatego proszę o wyrozumiałość. Moja aplikacja działa jako usługa pierwszego planu. Ma za zadanie sprawdzać, co 5 minut czy mikrofon jest używany. Odpowiada za to kod w pliku ForegroundService.java. Gdy mikrofon jest używany powinien zostać wyświetlony toast: "case1" lub "case2" oraz wysłany broadcast. Niestety, niezależnie od stanu mikrofonu, to się nie udaje. Kod w pliku ForegroundService.java jest wykonywany, ale rozpoznanie, czy mikrofon jest używany, jest błędne.
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btnStartService, btnStopService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartService = findViewById(R.id.buttonStartService);
btnStopService = findViewById(R.id.buttonStopService);
btnStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService();
}
});
btnStopService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService();
}
});
}
public void startService() {
if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.RECORD_AUDIO ) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions( this, new String[] { Manifest.permission.RECORD_AUDIO },
1 );
}
Intent serviceIntent = new Intent(this, ForegroundService.class);
serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android");
ContextCompat.startForegroundService(this, serviceIntent);
}
public void stopService() {
Intent serviceIntent = new Intent(this, ForegroundService.class);
stopService(serviceIntent);
}
}
ForegroundService.java
public class ForegroundService extends Service {
public static final String CHANNEL_ID = "ForegroundServiceChannel";
private Timer timer;
private TimerTask timerTask;
private Handler handler1 = new Handler();
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputExtra");
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Foreground Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
if(timer == null) {
timer = new Timer();
timerTask = new TimerTask() {
public void run() {
handler1.post(new Runnable() {
public void run() {
AudioRecord recorder;
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_DEFAULT, 44100);
try {
if (recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) {
Toast.makeText(getApplicationContext(), "Case1", Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.setAction("com.cfs.RANDOM_NUMBER_INTENT");
sendBroadcast(intent);
}
recorder.startRecording();
if (recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
recorder.stop();
Toast.makeText(getApplicationContext(), "case2", Toast.LENGTH_LONG).show();
Intent intent2 = new Intent();
intent2.setAction("com.cfs.RANDOM_NUMBER_INTENT2");
sendBroadcast(intent2);
}
recorder.stop();
} finally {
recorder.release();
}
}
});
}
};
timer.schedule(timerTask, 5000, 300000);
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if(timer != null){
timer.cancel();
timer.purge();
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wave.foregroundservice">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".ForegroundService"
android:enabled="true"
android:exported="true"></service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
dodana zawartość
Debugowałem aplikację (android 6.0) i
recorder.getRecordingState()
wynosi "1" niezależnie czy mikrofon jest używany przez inną aplikację, czy nie. Gdy dam
recorder.startRecording();
zmienia się na "3" również niezależnie od innych aplikacji.