Android陀螺仪传感器和推送通知

Android陀螺仪传感器和推送通知,android,push-notification,android-service,android-sensors,android-push-notification,Android,Push Notification,Android Service,Android Sensors,Android Push Notification,我使用带有Android服务的陀螺仪传感器。我有两个问题: 1-现在我想将传感器数据打印到主活动中的文本输入中。我已经定义了文本外观,但没有任何更改。服务启动时发送toast消息。然而,价值观​​在textview中不会被覆盖 2-同样,我想发布传感器值​​作为通知。我在编写传感器的服务中编写了通知系统,但它没有发送通知。我在网上查了一下,但找不到别的事可做 我尝试在不同的地方定义textview,但结果是一样的 请帮助我:( 传感器维修代码: package com.example.incli

我使用带有Android服务的陀螺仪传感器。我有两个问题:

1-现在我想将传感器数据打印到主活动中的文本输入中。我已经定义了文本外观,但没有任何更改。服务启动时发送toast消息。然而,价值观​​在textview中不会被覆盖

2-同样,我想发布传感器值​​作为通知。我在编写传感器的服务中编写了通知系统,但它没有发送通知。我在网上查了一下,但找不到别的事可做

我尝试在不同的地方定义textview,但结果是一样的

请帮助我:(

传感器维修代码:

package com.example.inclineproject;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.widget.TextView;
import android.widget.Toast;

public class SensorService extends Service implements SensorEventListener {
    float x,y,z;
    TextView textX, textY, textZ;
    private SensorManager sensorManager;
    private Sensor gyroscopeSensor;
    private SensorEventListener gyroscopeEventListener;
    private NotificationCompat.Builder builder;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "Servis Başladı..", Toast.LENGTH_SHORT).show();
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        gyroscopeSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Sensör Durduruldu..", Toast.LENGTH_SHORT).show();
        sensorManager.unregisterListener(this);
        super.onDestroy();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        textX.findViewById(R.id.textx);
        textY.findViewById(R.id.texty);
        textZ.findViewById(R.id.textz);

        Sensor sensor = event.sensor;
        if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            textX.setText("X : " + event.values[0]);
            textY.setText("Y : " + event.values[1]);
            textZ.setText("Z : " + event.values[2]);

            // Notify
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                String kanalId = "kanalId";
                String kanalAd ="kanalAd";
                String kanalTanım ="kanalTanım";
                int kanalOnceligi = NotificationManager.IMPORTANCE_HIGH;

                NotificationChannel kanal = notificationManager.getNotificationChannel(kanalId);

                if(kanal == null) {
                    kanal = new NotificationChannel(kanalId,kanalAd,kanalOnceligi);
                    kanal.setDescription(kanalTanım);
                    notificationManager.createNotificationChannel(kanal);
                }

                builder = new NotificationCompat.Builder(this,kanalId);
                builder.setContentTitle("Başlık");
                builder.setContentText("X : " + event.values[0] + " Y : " + event.values[1] + " Z : " + event.values[2]);

            } else {

                builder.setSmallIcon(R.drawable.ic_launcher_background);
                builder.setContentTitle("Eğim");
                builder.setContentText("X : " + event.values[0] + " Y : " + event.values[1] + " Z : " + event.values[2]);
                builder.setPriority(NotificationCompat.PRIORITY_HIGH);
                notificationManager.notify(1,builder.build());

            }

            notificationManager.notify(1,builder.build());


        }
    }

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

    }
}

代码有问题

textX.findViewById(R.id.textx);
textY.findViewById(R.id.texty);
textZ.findViewById(R.id.textz);
您没有正确获取textview引用。即使您从“活动”中正确传递了它们,从服务更改视图也是不正确的。活动的生命周期与服务不同,尝试引用不存在的textview肯定会导致崩溃

else {
    builder.setSmallIcon(R.drawable.ic_launcher_background);
    builder.setContentTitle("Eğim");
    builder.setContentText("X : " + event.values[0] + " Y : " + event.values[1] + " Z : " + event.values[2]);
    //...
}
如果查看此处的else块,则生成器对象甚至尚未实例化。您正在尝试调用对象上的实例方法。如果else块中的代码执行,则会调用NotificationManager.notify两次

我的两分钱:

  • 不要直接从服务更新文本视图,这是不安全的。如果需要,请通过intent将它们发送到活动

  • 可以通过服务发送通知,但如果可能,仍应避免发送。如果仍要这样做,请重构代码,并使用PendingEvent关联活动以处理通知

  • 示例代码

    添加示例代码,该代码需要一个名为NotificationView的单独活动来处理通知上的单击事件。如果不需要此依赖项,您可以删除与PendingEvent相关的代码。为了简洁起见,我还没有包含代码的任何其他部分

    注意,我在onCreate中创建了通知通道(仅一次)


    谢谢你的回答。我是Android服务新手。-我在顶部将Builder定义为一个全局变量。所以我直接从else块中的Builder开始。-除此之外,你有机会再解释一下你的建议吗?或者你有机会抛出一个示例源视频吗?因为我不太明白。这就是我为什么这么做的原因我脑子里想不出一个解决方案。@YılmazYaızDokumacı,我添加了一个代码片段供参考。希望这有帮助。我检查了。但是图片中的部分给出了一个错误。错误图片:正如我所说,如果不需要,可以删除与PendingEvent相关的代码。另外两个是字符串值,可以添加到您的项目。我添加了如下字符串:。但它不起作用。未发送通知。
    package com.example.inclineproject;
    
    //import...
    
    public class SensorService extends Service implements SensorEventListener {
        //...
        private NotificationCompat.Builder builder;
        private String CHANNEL_ID = "YOUR_CHANNEL_ID";
    
        @Override
        public void onCreate() {
            //...
            createNotificationChannel();
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
    
            Sensor sensor = event.sensor;
            if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
    
                // Notify
                createNotification(1, "Başlık", "X : " + event.values[0] + " Y : " + event.values[1] + " Z : " + event.values[2]);
            }
        }
    
        private void createNotification(int notificationId, String title, String content) {
            Intent intent = new Intent(this, NotificationView.class);
            intent.putExtra("notificationID", notificationId);
    
            PendingIntent pendingIntent = PendingIntent.getActivity(this,
                    0, intent, 0);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(android.R.drawable.ic_menu_my_calendar)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);
    
            NotificationManagerCompat nm = NotificationManagerCompat.from(this);
            nm.notify(notificationId, builder.build());
        }
    
        private void createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = getString(R.string.channel_name);
                String description = getString(R.string.channel_description);
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
                channel.setDescription(description);
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
        }
    }