gpt4 book ai didi

java - ShakeService.java 通过 Intent 调用后不打印 Log 消息

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:23 28 4
gpt4 key购买 nike

我试图在开关打开时通过 Intent 调用 ShackService.java,它会检测移动设备的加速度,当它摇动并显示 Toast 消息时,但它不起作用,那么我的代码中存在什么问题?

我已经尝试更改调用 Intent 部分的位置,并在代码的该部分中获取日志消息,该部分工作正常,但调用 Intent 后在 ShackService.java 中未获取日志消息。

MainActivity.java

package com.example.android.safetyapp;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private Switch OnOffSwitch;
private Boolean SwitchState;

private EditText messagebox;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

messagebox = (EditText) findViewById(R.id.messagebox);

OnOffSwitch = (Switch) findViewById(R.id.OnAndOffSwitch);
//Initialize switch
SwitchState = OnOffSwitch.isChecked(); //Current state of switch

if (SwitchState == false) { //to show initial text
OnOffSwitch.setText("Service is Off");
}

OnOffSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String emergencymessage = messagebox.getText().toString().trim();
if (emergencymessage.isEmpty()) {
OnOffSwitch.setChecked(false);
OnOffSwitch.setText("Service is Off");
Toast.makeText(MainActivity.this, "Message Box cannot leave empty", Toast.LENGTH_LONG).show();
} else {
if (OnOffSwitch.isChecked()) {

//start shake detection
Intent intent = new Intent(MainActivity.this, ShakeService.class);
//Start Service
startService(intent);

OnOffSwitch.setText("Service is On");
Toast.makeText(MainActivity.this, "Service starts", Toast.LENGTH_SHORT).show();

} else {
OnOffSwitch.setText("Service is Off");
Toast.makeText(MainActivity.this, "Service stopped", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}

ShakeService.java

package com.example.android.safetyapp;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import java.util.Random;

public class ShakeService extends Service implements SensorEventListener {

private static final String TAG = "myApp";

private SensorManager mSensorManager;
private Sensor mAccelerometer;
private float mAccel; // acceleration apart from gravity
private float mAccelCurrent; // current acceleration including gravity
private float mAccelLast; // last acceleration including gravity

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_UI, new Handler());
return START_STICKY;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;
// perform low-cut filter
if (mAccel > -1) {
Log.v(TAG, "did something");
Toast.makeText(ShakeService.this,"Shaking detected",Toast.LENGTH_SHORT).show();
mAccel=0;
}
}
}

我希望在调用 Intent 后打印日志消息“做了某事”,并在调用此 Intent 时打印“检测到摇晃”。

最佳答案

在 list 中注册服务 <service android:name=".ShakeService"/>

关于java - ShakeService.java 通过 Intent 调用后不打印 Log 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809310/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com