Android 服务应用程序

发布于 2024-10-21 00:18:47 字数 345 浏览 2 评论 0原文

我正在为 Android 设备编写一个软件套件,它将跟踪通话时间和各种其他通话信息。我希望其中一个应用程序作为服务运行,而无需从活动启动,并且我希望它在手机启动时立即初始化,并保持持续运行,监听电话,将信息记录到数据库中必要的。该服务需要在每次调用结束时向用户呈现一个对话框。

2 个问题:

  1. 如何让程序(服务)在启动时自动初始化,无需用户设置或交互?

  2. 我的印象是,如果不使用活动就无法实例化和显示对话框。我希望对话框显示在用户当前屏幕上的任何内容之上,并显示一个对话框。有没有办法使活动对当前活动完全透明,或者有没有办法从服务显示对话框?

提前致谢。

I'm writing a software suite for Android devices that will keep track of call times and various other call info. I'd like one of the applications to run as a Service without being started from an Activity, and I'd like it to initialize immediately when the phone boots, and stay running constantly, listening for phone calls, logging information to a database as necessary. The Service will need to present a dialog to the user at the end of each call.

2 questions:

  1. How do I get the program (Service) to initialize at boot automatically with no user setting or interaction required?

  2. I was under the impression that you cannot instantiate and display dialogs without using an Activity. I would like the dialogs to appear laid over whatever the user currently has on the screen, and display a dialog. Is there a way to make an Activity completely transparent over the current Activity or is there a way to display dialogs from a Service?

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2024-10-28 00:18:47

<块引用>

如何让程序(服务)在启动时自动初始化,无需用户设置或交互?

将此权限添加到您的清单中:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

然后将接收器添加到您的清单中:

<receiver android:name="your.package.BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.HOME" />
    </intent-filter>
</receiver>

在 Java 代码中创建接收器:

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // start the service from here
    }
}

<块引用>

我的印象是,如果不使用活动就无法实例化和显示对话框。我希望对话框显示在用户当前屏幕上的任何内容之上,并显示一个对话框。有没有办法使活动对当前活动完全透明,或者有没有办法从服务显示对话框?

是的,确实如此。您必须有一个弹出对话框的活动。要创建透明活动,请在 res/values/styles.xml 文件中添加以下样式(如果没有,请创建它。)这是一个完整的文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">#00000000</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

然后将该样式应用到您的活动,例如:

<activity android:name=".TransparentActivity" android:theme="@style/Theme.Transparent">
...
</activity>

How do I get the program (Service) to initialize at boot automatically with no user setting or interaction required?

Add this permission to your manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Then add a receiver to your manifest:

<receiver android:name="your.package.BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.HOME" />
    </intent-filter>
</receiver>

Create a receiver in Java code:

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // start the service from here
    }
}

I was under the impression that you cannot instantiate and display dialogs without using an Activity. I would like the dialogs to appear laid over whatever the user currently has on the screen, and display a dialog. Is there a way to make an Activity completely transparent over the current Activity or is there a way to display dialogs from a Service?

Yes, that's true. You have to have an activity that pops up the dialog. To create a transparent activity add the following style in your res/values/styles.xml file (if you don’t have one, create it.) Here’s a complete file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">#00000000</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

Then apply the style to your activity, for example:

<activity android:name=".TransparentActivity" android:theme="@style/Theme.Transparent">
...
</activity>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文