通知消息仅在设备连接到USB时显示 [英] notification message only shows when device connected to USB

查看:103
本文介绍了通知消息仅在设备连接到USB时显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中的通知消息字符串仅在开发设备连接到USB时显示。断开手机与USB的连接后,即使打开了应用程序,我的应用程序的通知也不再显示该消息,只是空白,但其他所有内容对于该通知似乎都可以正常工作。

The notification message string in my app only shows if the development device is connected to USB. As soon as I disconnect the phone from USB, even with the app open, the notifications for my app no longer shows the message, it is just blank, yet everything else seems to work fine for the notification.

在ADB中运行应用程序时,logcat中没有错误,并且所有其他参数已成功传递给通知。唯一的问题是setContentText()仅在手机通过USB连接到计算机的情况下才起作用,即使Android Studio / ADB没有运行也是如此。

I have no errors in logcat when running app in ADB, and all other params are passed successfully to the notification. The only issue is the setContentText() only seems to work if phone is connected to computer via USB, even if Android Studio/ADB is not running.

开发设备是运行4.1.2的旧三星SPH-L300(el-cheapo Virgin手机)。我没有其他设备可以对其进行测试。我将Android Studio 1.5.1用作IDE。

The dev device is an old Samsung SPH-L300 (el-cheapo Virgin mobile phone) running 4.1.2. I don't have another device to test it on. I am using Android Studio 1.5.1 as the IDE.

我缺少一些清单元素吗?我听说过类似的问题,但反过来说,它只有在未连接USB时才起作用。解决该问题的方法是更改​​gradle.build中的SDK最小值和目标,但没有解决我的问题(尽管尝试该方法时我可能会错过一些东西)。

Am I missing some manifest element? I've heard of a similar problem but it was the other way around, where it only worked when NOT connected to USB. The solution to that, being to change the SDK minimum and target in gradle.build, did not solve my problem (although, I could have missed something when trying that).

使用警报管理器的广播接收器激活该通知。...下面是代码:

The notification is activated using a broadcast receiver from an alarm manager....here is the code:

package com.example.notificationApp;

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.content.res.Resources;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {

    // get the info needed to identify this specific reminder...
    Bundle bundle = intent.getExtras()
    int alarmID = bundle.getInt("reminderID");

    Intent returnIntent = new Intent(context, HomeActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(context, alarmID, returnIntent, 0);

    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    // make vibrate pattern...
    long[] pattern = {500,500,500,500,500,500,500,500,500};

    Notification notify = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setTicker("Appointment reminder")
            .setContentTitle("Reminder...")
            .setContentText("TEST TEXT")
            .setSound(alarmUri)
            .setLights(Color.BLUE, 500, 500)
            .setVibrate(pattern)
            .setContentIntent(pIntent)
            .setAutoCancel(true)
            .build();

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

    notificationManager.notify(alarmID, notify);
}

这是我的Manifest.xml:

And here is my Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationApp">

<!-- Permission to start Alarm on device reboot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="23" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".HomeActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".AutoStart">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <receiver android:name=".CheckAlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <receiver android:name=".AlarmReceiver" />

    <!--
         ATTENTION: This was auto-generated to add Google Play services to your project for
         App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
    -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

我很困惑,有人有任何线索吗?

I am stumped on this one, anyone have any clues? Thanks in advance!!

推荐答案

所以,我发现了这个问题,并希望将其发布,以防其他人遇到同样的问题。信不信由你,问题出在手机的开发人员设置中,处于保持唤醒状态。在开发人员设置中关闭保持唤醒状态,瞧!

So I found the problem and wanted to post it in case anyone else ends up with the same issue. Believe it or not, the problem was in the phone's developer settings with Stay Awake mode on. Turn off Stay Awake in developer settings and voila!

我从刚刚发现的另一篇文章中得到了这个主意:

I got the idea from this other post I just found:

https://stackoverflow.com/a/25566924/3716557

(请直接向链接中的答案)

(Please direct any upvotes to the answer in link)

这篇关于通知消息仅在设备连接到USB时显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆