您好,我正在开发一个LED闪光警报应用程序,该LED会在通话时闪烁,并且短信可以在所有版本中正常运行,但在Android O 8.0或更高版本中无法正常使用,请发送任何解决方案。非常感谢 Almost same as this app Flash Alert < / p>
Flash Alert Class (闪光警报类):打开和关闭闪光灯:
package com.flashalert.FLash;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.SystemClock;
import android.util.Log;
public class FlashThread implements Runnable {
private static Camera cam;
private static Parameters params;
private boolean isFinish;
private boolean isFlashOn;
private Context mContext;
private int offLength;
private int onLength;
private int times;
public FlashThread(Context context, int onLength, int offLength, int times) {
this.isFinish = true;
this.mContext = context;
this.isFinish = true;
this.times = times;
this.onLength = onLength;
this.offLength = offLength;
}
public boolean isFlashEnable() {
return this.isFinish;
}
public void setFlashEnable(boolean enable) {
this.isFinish = enable;
}
public int getOnLength() {
return this.onLength;
}
public void setOnLength(int onLength) {
this.onLength = onLength;
}
public int getOffLength() {
return this.offLength;
}
public void setOffLength(int offLength) {
this.offLength = offLength;
}
public int getCount() {
return this.times;
}
public void setCount(int count) {
this.times = count;
}
private void getCamera() {
if (cam == null) {
try {
cam = Camera.open();
params = cam.getParameters();
//cam.startPreview();
} catch (RuntimeException e) {
}
}
}
public void turnOffFlash() {
if (!this.isFlashOn || cam == null || params == null) {
Log.e("CallService", "turnOffReturn");
return;
}
try {
params=cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
// params.setFlashMode("off");
cam.setParameters(params);
cam.stopPreview();
this.isFlashOn = false;
} catch (RuntimeException e) {
}
}
public void turnOnFlash() {
if (this.isFlashOn || cam == null || params == null) {
Log.e("CallService", "turnOnReturn");
return;
}
try {
params=cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(params);
cam.startPreview();
this.isFlashOn = true;
} catch (RuntimeException e) {
}
}
public void run() {
Log.e("CallService", "thread run");
getCamera();
int i = 0;
while (i < this.times) {
if (this.isFinish) {
try {
turnOnFlash();
} catch (Exception e) {
}
SystemClock.sleep((long) this.onLength);
try {
turnOffFlash();
} catch (Exception e2) {
}
SystemClock.sleep((long) this.offLength);
i++;
} else {
Log.e("CallService", "break thread");
turnOffFlash();
try {
cam.stopPreview();
cam.release();
cam = null;
return;
} catch (Exception e3) {
destroy();
return;
}
}
}
try {
Log.e("CallService", "try break");
cam.stopPreview();
cam.release();
cam = null;
} catch (Exception e4) {
Log.e("CallService", "catch break");
destroy();
}
}
public void destroy() {
try {
cam.stopPreview();
cam.release();
cam = null;
} catch (Exception e) {
}
}
}
Flash Alert呼叫接收器类别,以在呼叫响铃时运行LED:
package com.flashalert.Receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import span4u.flashalert.Services.CallService;
//import com.iodev.flashalert.common.PrefUtils;
//import com.iodev.flashalert.common.Utils;
//import com.iodev.flashalert.service.CallService;
public class PhoneReceiver extends BroadcastReceiver {
private static final String TAG = "PhoneReceiver";
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "onReceive call");
if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) {
return;
}
// if (Utils.getInstance(context.getApplicationContext()).checkSetup(context.getApplicationContext(), PrefUtils.TYPE_CALL)) {
try {
context.startService(new Intent(context, CallService.class).setAction(intent.getAction()).putExtra("incoming_number", intent.getStringExtra("incoming_number")).putExtra("state", intent.getStringExtra("state")));
}
catch (IllegalStateException e){
e.printStackTrace();
}
// } else {
// Log.e(TAG, "Receiver return");
// }
// if (intent.getStringExtra("state").equals(TelephonyManager.EXTRA_STATE_RINGING)) {
// flashThread = new FlashThread(context, 300, 300, 20);
// flashThread.setFlashEnable(true);
// new Thread(flashThread).start();
//
// } else {
// flashThread.setFlashEnable(false);
// new Thread(flashThread).stop();
// }
//System.out.print("state:call" );
// System.out.print("state:" + intent.getStringExtra("state"));
}
}
我的清单类:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flashalert">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera2" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".App"
android:theme="@style/AppTheme">
<activity android:name=".SettingsActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Receiver.MessagingReceiver"
android:exported="true"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="5822">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<receiver android:name=".Receiver.PhoneReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".Services.CallService"
android:enabled="true"/>
<service android:name=".Services.MessageService"
android:enabled="true"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- Include the AdActivity configChanges and theme. -->
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
<activity android:name=".MainActivity"
android:screenOrientation="portrait"></activity>
</application>
</manifest>