【问题标题】:Android Notification App安卓通知应用
【发布时间】:2011-03-31 11:23:49
【问题描述】:

我目前正在开发一个安卓应用程序。每当用户安装/下载新的第三方应用程序时,我都必须记录任何新安装的应用程序名称。如果用户正在安装新应用程序,我如何获得通知。提前致谢。

Java 文件

public class ApplicationBroadcastService extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        System.out.print("-------");
    }
}

清单

    <receiver android:name=".applicationlog.ApplicationBroadcastService">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED"  />
            <action android:name="android.intent.action.PACKAGE_CHANGED" />
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
        </intent-filter>
     </receiver>

但是当我安装/卸载任何应用程序时,我仍然没有输入 onReceive 方法。

解决办法如下:

我对 Manifest 文件做了一些小改动。

    <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.PACKAGE_ADDED"  />
            <action android:name="android.intent.action.PACKAGE_CHANGED" />
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" />
        </intent-filter>

现在它工作正常.. :) 再次感谢@willytate

【问题讨论】:

  • 您的代码中是否有任何启动器活动?请您分享您的整个代码,因为我尝试了您的代码,但它不适合我。
  • 如果应用程序未在后台运行,如何触发 onReceive。如果应用程序正在运行用于 Package_ADDED 和 REMOVED 工作的 onReceive 方法。但我需要触发即使应用程序未运行,请帮助我

标签: android android-activity notifications


【解决方案1】:

阿杰,

您需要设置一个带有意图过滤器的BroadcastReceiver 以接收以下操作:ACTION_PACKAGE_ADDED 然后从 BroadcastReceiver 的onReceive() 方法中您可以启动一个Notification

【讨论】:

  • 谢谢@willytate。完美的帮助。
  • @willylate,请看我的代码。我仍然没有进入 onReceive 方法。如果我做错了什么,请纠正我。
  • 好吧,我知道要接收PACKAGE_REMOVED 你需要BROADCAST_PACKAGE_REMOVED 的权限。尝试将接收器线路设置为通常适用的&lt;receiver android:name=".ApplicationBroadcastService"&gt;
  • 我怎样才能得到上次安装的应用程序的名称或信息...谢谢
  • 您可以尝试使用在此意图期间与数据一起传入的 EXTRA_UID 值。它应该为您提供最后安装的应用程序的 ID 值。使用此信息从PackageManager 检索有关应用程序的更多信息。这只是一个有根据的猜测......
【解决方案2】:

看看intent documentation。您正在寻找ACTION_PACKAGE_INSTALL(似乎从未使用过,参见 cmets)和ACTION_PACKAGE_REMOVED

【讨论】:

  • 不,尝试实现 willytate 提到的内容。没有人会为你写代码。如果你卡住了,问一个新问题(不要在那里问代码,展示你所拥有的......)
  • 感谢@WarrenFaith,您的回答对我来说不是很清楚。这就是为什么我要求提供一些线索。无论如何感谢您的回复。
  • 文档说 ACTION_PACKAGE_INSTALL 从未使用过。 developer.android.com/reference/android/content/…
  • 感谢您说,在撰写本文时,文档中没有提及...
【解决方案3】:

您可以监听android.intent.action.PACKAGE_ADDED 意图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2017-12-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多