问题描述
这是前台服务的类 包 com.example.mrgenz2;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.notificationmanager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import com.intentfilter.androidpermissions.PermissionManager;
public class foreground_emergency extends Service {
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
final PermissionManager permissionManager = PermissionManager.getInstance(this);
create_notification_channel();
Intent intent1 = new Intent(this,MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,intent1,0);
Notification noti = new NotificationCompat.Builder(this,"channel_id")
.setContentTitle("foreground tutorial")
.setContentText("the application is running")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pi)
.build();
startForeground(1,noti);
return START_STICKY;
}
private void create_notification_channel()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel nc = new NotificationChannel(
"channel_id","foreground notification",notificationmanager.IMPORTANCE_DEFAULT
);
notificationmanager manager = getSystemService(notificationmanager.class);
manager.createNotificationChannel(nc);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
stopForeground(true);
stopSelf();
super.onDestroy();
}
}
这是来自另一个类的motionSensor类
public class vibrator extends broadcastReceiver implements SensorEventListener {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
enter code here
final TextView accel_1 = stand_by_floating_view.findViewById(R.id.acceralate_1);
final TextView accel_2 = stand_by_floating_view.findViewById(R.id.acceralate_2);
final TextView accel_3 = stand_by_floating_view.findViewById(R.id.acceralate_3);
accel_1.setText(sensorEvent.values[0]+"m/s2");
accel_2.setText(sensorEvent.values[1]+"m/s2");
accel_3.setText(sensorEvent.values[2]+"m/s2");
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
current_accel_1 = sensorEvent.values[0];
current_accel_2 = sensorEvent.values[1];
current_accel_3 = sensorEvent.values[2];
if(itIsNotFirstTime)
{
accel_1_diff = Math.abs(last_accel_1 - current_accel_1);
accel_2_diff = Math.abs(last_accel_2 - current_accel_2);
accel_3_diff = Math.abs(last_accel_3 - current_accel_3);
if((accel_1_diff > shakethreshold && accel_2_diff > shakethreshold) || (accel_1_diff > shakethreshold && accel_3_diff > shakethreshold) || (accel_2_diff > shakethreshold && accel_3_diff > shakethreshold))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneshot(500,VibrationEffect.DEFAULT_AMPLITUDE));
}
else
{
vibrator.vibrate(500);
}
}
}
last_accel_1 = current_accel_1;
last_accel_2 = current_accel_2;
last_accel_3 = current_accel_3;
itIsNotFirstTime = true;
}
@Override
public void onAccuracyChanged(Sensor sensor,int accuracy) {
}
@Override
protected void onResume() {
super.onResume();
if(isAccelerometerSensorAvailable)
sensorManager.registerListener(this,accelerometerSensor,SensorManager.SENSOR_DELAY_norMAL);
}
@Override
protected void onPause() {
super.onPause();
if(isAccelerometerSensorAvailable)
sensorManager.unregisterListener(this);
}
}
我想知道有没有办法将振动器类归类到 foreground_emergency 类,我尝试粘贴它,但是振动器类中的 onResume 和 onPause 函数在扩展服务的 foreground_emergency 中无效
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)