在真实设备上多次振动以获取 Android 中的通知(近似警报)

发布于 2025-01-03 20:46:09 字数 2762 浏览 0 评论 0原文

我已经设置了当用户进入特定位置时的通知。当用户输入 Radius Phone 在通知栏显示通知并一次又一次振动时会发生什么。即使我清除通知手机仍保持振动,请帮助我! 这是通知代码:

import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


    public class ProximityIntentReceiver extends BroadcastReceiver {

    private static int NOTIFICATION_ID = 0;
    public static final String EVENT_ID_INTENT_EXTRA = "EventIDIntentExtraKey";
    public static final String PLACE_NAME = "PLACE";
    public static final String ITEM_NAME = "ITEM";
    String Place_Name = "",Item_Name = "";


    @Override
    public void onReceive(Context context, Intent intent) {
         Log.v("entered","" );
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        //Bundle extras = getIntent().getExtras(); 
        NOTIFICATION_ID = intent.getIntExtra(EVENT_ID_INTENT_EXTRA, -1);
        Place_Name = intent.getStringExtra(PLACE_NAME);
        Item_Name =  intent.getStringExtra(ITEM_NAME);
        Boolean entering = intent.getBooleanExtra(key, false);
         Log.v(Place_Name,Item_Name );

        if (entering) {
            Log.d(getClass().getSimpleName(), "entering");
        }
        else {
            Log.d(getClass().getSimpleName(), "exiting");
        }


        NotificationManager notificationManager = 
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);       

        Notification notification = createNotification();
        notification.setLatestEventInfo(context, "Hello You are near to this", Place_Name + "  and your task is  " + Item_Name, pendingIntent);

        notificationManager.notify(NOTIFICATION_ID, notification);

    }

    private Notification createNotification() {
        Notification notification = new Notification();

        notification.icon = R.drawable.newnotification;
        notification.when = System.currentTimeMillis();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;

        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        notification.ledARGB = Color.WHITE;
        notification.ledOnMS = 1500;
        notification.ledOffMS = 1500;

        return notification;
    }
}

我做错了什么?

i have set a notification when user enters in specific location. what happen when user enter the radius phone show notification at notification bar and vibrate again and again. even i clear the notification phone keep vibration please help me out!
here is code for notification:

import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


    public class ProximityIntentReceiver extends BroadcastReceiver {

    private static int NOTIFICATION_ID = 0;
    public static final String EVENT_ID_INTENT_EXTRA = "EventIDIntentExtraKey";
    public static final String PLACE_NAME = "PLACE";
    public static final String ITEM_NAME = "ITEM";
    String Place_Name = "",Item_Name = "";


    @Override
    public void onReceive(Context context, Intent intent) {
         Log.v("entered","" );
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        //Bundle extras = getIntent().getExtras(); 
        NOTIFICATION_ID = intent.getIntExtra(EVENT_ID_INTENT_EXTRA, -1);
        Place_Name = intent.getStringExtra(PLACE_NAME);
        Item_Name =  intent.getStringExtra(ITEM_NAME);
        Boolean entering = intent.getBooleanExtra(key, false);
         Log.v(Place_Name,Item_Name );

        if (entering) {
            Log.d(getClass().getSimpleName(), "entering");
        }
        else {
            Log.d(getClass().getSimpleName(), "exiting");
        }


        NotificationManager notificationManager = 
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);       

        Notification notification = createNotification();
        notification.setLatestEventInfo(context, "Hello You are near to this", Place_Name + "  and your task is  " + Item_Name, pendingIntent);

        notificationManager.notify(NOTIFICATION_ID, notification);

    }

    private Notification createNotification() {
        Notification notification = new Notification();

        notification.icon = R.drawable.newnotification;
        notification.when = System.currentTimeMillis();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;

        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        notification.ledARGB = Color.WHITE;
        notification.ledOnMS = 1500;
        notification.ledOffMS = 1500;

        return notification;
    }
}

what im doing wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桜花祭 2025-01-10 20:46:09

试试这个:

notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.FLAG_ONLY_ALERT_ONCE;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;

Try this:

notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.FLAG_ONLY_ALERT_ONCE;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
红颜悴 2025-01-10 20:46:09

notification.when 用于告诉系统在什么时间发出通知。

notification.when is used to tell the system at what time to give the notification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文