我正在开发一个需要关闭的嵌入式应用程序之一,如果我通过代码隐藏了应用程序的图标,那么当我单击系统灰色通知启动它时,不会调用applicaton。
>我已经尝试在示例应用程序中对此进行模拟,但它甚至在这里也无法正常工作。没有启动器图标的Google for Apps是否有限制,不能从通知中调用?
我的Applicaiton类代码:
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
hideAppIcon()
showNotification()
}
private fun hideAppIcon() {
val p = packageManager
val componentName = ComponentName(
this,com.icon.notification.rel.Mainactivity::class.java
) // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
p.setComponentEnabledSetting(
componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP
)
}
private fun showNotification(){
// Create the NotificationChannel,but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_name)
val descriptionText = getString(R.string.channel_description)
val importance = Notificationmanager.IMPORTANCE_DEFAULT
val channel = NotificationChannel("local_notification",name,importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationmanager: Notificationmanager =
getSystemService(Context.NOTIFICATION_SERVICE) as Notificationmanager
notificationmanager.createNotificationChannel(channel)
// Create an explicit intent for an activity in your app
val intent = Intent(this,Mainactivity::class.java).apply {
flags = Intent.flaG_actIVITY_NEW_TASK or Intent.flaG_actIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getactivity(this,intent,0)
val builder = NotificationCompat.Builder(this,"local_notification")
.setSmallIcon(android.R.drawable.ic_dialog_email)//@android:drawable/ic_dialog_email
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setautoCancel(true)
with(NotificationmanagerCompat.from(this)) {
// notificationId is a unique int for each notification that you must define
notify(1000000001,builder.build())
}
}
}
}
我的活动代码:
class Mainactivity : AppCompatactivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportactionBar(toolbar)
fab.setOnClicklistener { view ->
this.finish()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main,menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button,so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<application
android:name=".MainApplication"
android:allowBackup="true"
android:theme="@style/Apptheme">
<activity
android:name=".Mainactivity"
android:theme="@style/Apptheme.NoactionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>