Android应用程序中服务的自动启动

发布于 2024-12-03 18:56:37 字数 3697 浏览 9 评论 0原文

我正在尝试开发简单的应用程序(服务),它将在启动时自动启动。主要活动还创建了新服务(我可以在服务任务管理器列表中看到它)。该服务也应该在重新启动我的手机(带有 android 2.3.4 的三星 Galaxy Ace)后创建,而不启动应用程序,但事实并非如此 - 我在服务任务管理器列表中看不到它。哪里可能有问题?这是我的代码:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cz.nafik.testService"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TestServiceActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService"
                 android:icon="@drawable/ic_launcher"
                 android:label="ledNotifier"
                 android:enabled="true">
            <intent-filter>
                <action android:name="cz.nafik.testService.MyService" />
            </intent-filter>
        </service>
        <receiver android:name=".BootUpReceiver" 
                  android:enabled="true"
                  android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>     
    </application>
</manifest>

MyService.java

package cz.nafik.testService;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(this, "service bind", Toast.LENGTH_LONG).show();
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "service destroyed", Toast.LENGTH_LONG).show();
    }

}

BootUpReceiver.java

package cz.nafik.testService;

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

public class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        intent.setAction("cz.nafik.testService.MyService");
        context.startService(intent);

    }

}

TestServiceActivity.java

package cz.nafik.testService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class TestServiceActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent serviceIntent = new Intent();
        serviceIntent.setAction("cz.nafik.testService.MyService");
        this.startService(serviceIntent);

    }
}

I am trying to develop simple app (service) which will start automatically on boot. With main activity is also created new service (I can see it in service task manager list). This service should be also created after rebooting my phone (Samsung Galaxy Ace with android 2.3.4) without launching application , but it is not - I can't see it in service task manager list. Where can be problem? Here are my codes:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cz.nafik.testService"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TestServiceActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService"
                 android:icon="@drawable/ic_launcher"
                 android:label="ledNotifier"
                 android:enabled="true">
            <intent-filter>
                <action android:name="cz.nafik.testService.MyService" />
            </intent-filter>
        </service>
        <receiver android:name=".BootUpReceiver" 
                  android:enabled="true"
                  android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>     
    </application>
</manifest>

MyService.java

package cz.nafik.testService;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(this, "service bind", Toast.LENGTH_LONG).show();
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "service destroyed", Toast.LENGTH_LONG).show();
    }

}

BootUpReceiver.java

package cz.nafik.testService;

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

public class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        intent.setAction("cz.nafik.testService.MyService");
        context.startService(intent);

    }

}

TestServiceActivity.java

package cz.nafik.testService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class TestServiceActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent serviceIntent = new Intent();
        serviceIntent.setAction("cz.nafik.testService.MyService");
        this.startService(serviceIntent);

    }
}

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

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

发布评论

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

评论(4

诗酒趁年少 2024-12-10 18:56:37

您混淆了清单文件中的接收者和权限。

  • 您应该删除文件开头的这一行:

    >
    
  • 并且您应该将接收器更改为这个更简单的版本:

    <接收器 android:name=".BootUpReceiver">
        <意图过滤器>
            >
        
         
    

这样它应该可以工作,除非除了上面解释的问题之外还有其他问题。

You have mixed up the receiver and the permissions in your manifest file.

  • You should remove this line, at the beginning of the file:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
  • And you should change your receiver to this simpler version:

    <receiver android:name=".BootUpReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>     
    

This way it should work, unless there is another problem on top of those explained above.

表情可笑 2024-12-10 18:56:37

几天前我问过几乎同样的问题。您可以在这里找到它:Android 自动启动应用程序

只需按照示例代码即可使其正常工作。您已经用 BroadcastReceiver 和权限弄乱了一些东西。

I've asked almost the same question few days ago. You can find it here: Android autostart application

Just follow the sample code to get it working. You've messed few things with BroadcastReceiver and permissions.

又怨 2024-12-10 18:56:37

尝试删除:

android:permission="android.permission.RECEIVE_BOOT_COMPLETED"

也可能

<category android:name="android.intent.category.DEFAULT" />

BootUpReceiver 的清单声明中删除。

我没有发现在我自己的应用程序中执行相同操作所必需的任何一个。

另外,我不知道在 BootupReceiver 代码中重新使用 BOOT_COMPLETED 意图会产生什么影响。只需创建一个新的,例如:

public class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent si = new Intent();
        si.setAction("cz.nafik.testService.MyService");
        context.startService(si);
    }

}

Try removing:

android:permission="android.permission.RECEIVE_BOOT_COMPLETED"

and maybe also

<category android:name="android.intent.category.DEFAULT" />

from your manifest declaration for BootUpReceiver.

I did not find either of those necessary to do the same in my own application.

Also, I don't know what the effects of re-using the BOOT_COMPLETED intent in your BootupReceiver code. Just create a new one, e.g.:

public class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent si = new Intent();
        si.setAction("cz.nafik.testService.MyService");
        context.startService(si);
    }

}
扎心 2024-12-10 18:56:37

我认为问题在于您用来启动服务的意图:

    intent.setAction("cz.nafik.testService.MyService");
    context.startService(intent);

这不会启动服务 cz.nafik.testService.MyService 而是调用操作“cz.nafix...”关于意图的先前接收者。您可能希望显式地将服务的类设置为新意图的接收者,并使操作特定于该服务。

不要尝试重用您在广播中收到的意图,而是创建一个新意图并使用它:

Intent startMyService = new Intent( context, MyService.class );
startMyService.setAction( "MAKE_TOAST" );
context.startService( startMyService );

然后在服务中,您可以检查要执行的操作:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if( "MAKE_TOAST".equals( intent.getAction() ) )
    {
        Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent,flags,startId);
    }
}

I think the problem is with the intent you're using to start the service:

    intent.setAction("cz.nafik.testService.MyService");
    context.startService(intent);

This will not start the service cz.nafik.testService.MyService but rather invoke the action "cz.nafix..." on the previous recipients of the intent. You probably want to explicitly set the service's class as the recipient of a new intent, and make the action specific to that service.

Rather than trying to reuse the intent you've received as part of the broadcast, create a new intent and use that instead:

Intent startMyService = new Intent( context, MyService.class );
startMyService.setAction( "MAKE_TOAST" );
context.startService( startMyService );

Then in the service, you can check what action to perform:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if( "MAKE_TOAST".equals( intent.getAction() ) )
    {
        Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent,flags,startId);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文