Android-启动时启动服务

发布于 2021-02-02 22:57:41

从我在Stack Exchange和其他地方看到的所有内容中,我已经正确设置了所有内容,可以在启动Android OS时启动IntentService。不幸的是,它没有在启动时启动,并且我没有收到任何错误。也许专家可以帮忙…

表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.phx.batterylogger"
  android:versionCode="1"
  android:versionName="1.0"
  android:installLocation="internalOnly">

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <service android:name=".BatteryLogger"/>
    <receiver android:name=".StartupIntentReceiver">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED" />  
        </intent-filter>  
    </receiver>
</application>

</manifest>

用于启动的BroadcastReceiver:

package com.phx.batterylogger;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartupIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent serviceIntent = new Intent(context, BatteryLogger.class);
        context.startService(serviceIntent);
    }
}

更新:我尝试了下面的几乎所有建议,并Log.v("BatteryLogger", "Got to onReceive, about to start service");在StartupIntentReceiver的onReceive处理程序中添加了日志记录,但从未记录过任何内容。因此,它甚至还没有到达BroadcastReceiver

我认为我正在部署APK并正确测试,只是在Eclipse中运行Debug,控制台说它已成功将其安装到我的Xoom平板电脑上,位于\ BatteryLogger \ bin \ BatteryLogger.apk。然后进行测试,我重新启动平板电脑,然后查看DDMS中的日志,并在OS设置中检查“正在运行的服务”。这听起来是否正确,还是我缺少了什么?再次感谢你的帮助。

关注者
0
被浏览
694
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    好吧,这是一个自动启动应用程序的完整示例

    AndroidManifest文件

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="pack.saltriver" android:versionCode="1" android:versionName="1.0">
    
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
        <application android:icon="@drawable/icon" android:label="@string/app_name">
    
            <receiver android:name=".autostart">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
    
            <activity android:name=".hello"></activity>
            <service android:enabled="true" android:name=".service" />
        </application>
    </manifest>
    

    自动启动

    public class autostart extends BroadcastReceiver 
    {
        public void onReceive(Context context, Intent arg1) 
        {
            Intent intent = new Intent(context,service.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(intent);
            } else {
                context.startService(intent);
            }
            Log.i("Autostart", "started");
        }
    }
    service.java
    
    public class service extends Service
    {
        private static final String TAG = "MyService";
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
        public void onDestroy() {
            Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onDestroy");
        }
    
        @Override
        public void onStart(Intent intent, int startid)
        {
            Intent intents = new Intent(getBaseContext(),hello.class);
            intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intents);
            Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onStart");
        }
    }
    

    hello.java-执行一次Applicaton后,每次启动设备时都会弹出此窗口。

    public class hello extends Activity 
    {   
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
        }
    }
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看