【问题标题】:Showing notification in Android wear在 Android Wear 中显示通知
【发布时间】:2016-12-29 10:19:17
【问题描述】:
 private void notification() {

    NotificationManagerCompat manager = NotificationManagerCompat.from(getApplication());

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplication());
    builder.setAutoCancel(false);
    builder.setSmallIcon(R.mipmap.ic_launcher);

    Notification notification = builder.getNotification();
    notification.flags |= Notification.FLAG_ONGOING_EVENT; 
    notification.flags |= Notification.FLAG_NO_CLEAR; 
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    builder.setContentTitle(getResources().getString(R.string.app_name));

    if (WearableListener.STATE_CONNECTED == WearableManager.getInstance().getConnectState()) {
        builder.setContentText(getResources().getString(R.string.connected));
        builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
    } else {
        builder.setContentText(getResources().getString(R.string.not_connected));
    }

    Intent intent = new Intent(this, homepage.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    manager.notify(R.mipmap.ic_launcher, builder.build());
}

private static final Intent ACCESSIBILITY_INTENT = new Intent("android.settings.ACCESSIBILITY_SETTINGS");
public static final Intent NOTIFICATION_LISTENER_INTENT = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");

private WearableListener wearableListener = new WearableListener() {
    @Override
    public void onConnectChange(int oldState, int newState) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateMainActivity();
            }
        });

        bluetoothConnectStateSave = newState;
        Log.e(TAG, "WearableListener  newState=" + newState);
        switch (newState) {
            case WearableListener.STATE_NONE://0
                bluetoothConnectState = newState;
                break;
            case WearableListener.STATE_CONNECTING://2
                pview.removeCallbacks(timeRunnable);
                bluetoothConnectState = newState;
                count = 0;
                count_max = COUNT_MAX - CONNECT_DELAY;
                count_try_disconnect = 0;
                pview.post(timeRunnable);
                break;
            case WearableListener.STATE_CONNECTED://3
                handler.removeMessages(HANDLE_MESSAGE_RECONNECT_AUTO);
                bluetoothConnectState = newState;
                pview.removeCallbacks(timeRunnable);
                notification();
                break;
            case WearableListener.STATE_CONNECT_FAIL://4
            case WearableListener.STATE_CONNECT_LOST://5
            case WearableListener.STATE_DISCONNECTING://6
                notification();
                break;
        }
        Log.e(TAG, "WearableListener bluetoothConnectState=" + bluetoothConnectState + "   bluetoothConnectStateSave=" + bluetoothConnectStateSave);
        Log.e(TAG, "WearableListener count=" + count + "   count_max=" + count_max);
    }

    @Override
    public void onDeviceChange(BluetoothDevice bluetoothDevice) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateMainActivity();
            }
        });
    }

    @Override
    public void onDeviceScan(BluetoothDevice bluetoothDevice) {

    }

    @Override
    public void onNameChange(String s) {
    }
};
private void updateMainActivity() {
    device = WearableManager.getInstance().getRemoteDevice();
}

以上是项目中的代码,是蓝牙低功耗连接和通知部分。我可以在 android wear(手表显示“正在听”,这意味着它们已连接)和 android 手机之间进行连接。但是,通知仅显示在手机中,我无法在 android wear 中收到通知。我的项目有什么问题?请给我一些帮助。非常感谢。

【问题讨论】:

    标签: android bluetooth-lowenergy android-notifications wear-os android-wear-notification


    【解决方案1】:

    你试过检查这个support page吗?您可以尝试给出的故障排除步骤,然后在每个步骤后检查您的手表,以查看通知是否开始工作。​如果还是不行,试试resetting your watch to factory settings。重启手表在短期内似乎对大多数人有用,但problem can re-occur 有时很快。您还可以查看此Android Wear notifications issues forum 并查看给定的解决方法是否会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多