首页 文章

通知消息仅显示设备连接到USB的时间

提问于
浏览
0

我的应用程序中的通知消息字符串仅显示开发设备是否连接到USB . 一旦我断开手机与USB的连接,即使应用程序打开,我的应用程序的通知也不再显示消息,它只是空白,但其他所有内容似乎都能正常通知 .

在ADB中运行应用程序时,我在logcat中没有错误,并且所有其他参数都成功传递给通知 . 唯一的问题是,如果手机通过USB连接到计算机,即使Android Studio / ADB未运行,setContentText()似乎也能正常工作 .

开发设备是运行4.1.2的旧三星SPH-L300(el-cheapo Virgin手机) . 我没有其他设备来测试它 . 我使用Android Studio 1.5.1作为IDE .

我错过了一些明显的元素吗?我听说过类似的问题,但它是相反的,只有在没有连接到USB的情况下它才能工作 . 解决方案是,在gradle.build中更改SDK最小值和目标,并没有解决我的问题(虽然,我可能在尝试时错过了一些东西) .

使用来自警报管理器的广播接收器激活通知....这是代码:

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:

<?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>

我对这个很难过,有人有任何线索吗?提前致谢!!

1 回答

  • 1

    所以我发现了问题,并希望发布它以防其他人最终遇到同样的问题 . 信不信由你,问题出在手机的开发人员设置中,保持唤醒模式开启 . 在开发人员设置中关闭Stay Awake并瞧!

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

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

    (请在链接中指出任何upvotes到答案)

相关问题