繁体   English   中英

Android - 从通知启动活动

[英]Android - Launching Activity from Notification

我正在编写一个时间跟踪器应用程序,它启动一个未绑定的前台服务,以让用户了解经过的时间。 该服务运行顺利,一切都像一个魅力......除了一件事。 当用户点击通知时,应用程序的主要活动应该开始。 根据 Androids 文档( https://developer.android.com/training/notify-user/navigation )此代码应该可以工作,但它只是启动应用程序的设置 Activity 的 ZE84E30B9390CDB64DB6DB2C9AB87846D。

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    Log.d(TAG, "Started")
    isRunning = true
    val channelID = createNotificationChannel()
    val pendingIntent: PendingIntent = Intent(this, MainActivity::class.java).let { notificationIntent ->
        PendingIntent.getActivity(this, 0, notificationIntent, FLAG_UPDATE_CURRENT)
    }

    val notification: Notification = NotificationCompat.Builder(this, channelID)
        .setContentTitle(CHANNEL_NAME)
        .setContentText("My wonderful Text")
        .setPriority(PRIORITY_LOW)
        .setContentIntent(pendingIntent)
        .build()
    startForeground(FOREGROUND_ID, notification)
    timer.scheduleAtFixedRate(TimedTask(), 0, 1000)
    return super.onStartCommand(intent, flags, startId)
}

private fun createNotificationChannel(): String{
    val chan = NotificationChannel(CHANNEL_ID,
        CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE)
    chan.lightColor = Color.BLUE
    chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
    val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    service.createNotificationChannel(chan)
    return CHANNEL_ID
}

manifest.xml 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.maybe.tima">
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".TimerService"
            android:label="@string/app_name"
            />
    </application>
</manifest>

任何想法为什么会发生这种情况? 非常感谢您的帮助:)

我为使您的代码正常工作所做的唯一更改是在您的 notificationBulider 中添加方法“setSmallIcon”(但我在官方文档中找不到任何关于这种方法的这种影响的提及):

val notification: Notification = NotificationCompat.Builder(this, channelID)
        .setContentTitle(CHANNEL_NAME)
        .setContentText("My wonderful Text")
        .setPriority(PRIORITY_LOW)

        .setSmallIcon(R.drawable.ic_launcher_background) // line added

        .setContentIntent(pendingIntent)
        .build()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM