设备启动完成后启动服务

发布于 2024-12-27 06:18:52 字数 1918 浏览 0 评论 0原文

我正在做一个应用程序,我想在设备启动完成时拨打一个号码。我的代码是这样的:

 @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("**inside onRecevier");

        Intent serviceIntent = new Intent();
        serviceIntent.setAction("com.test.app.TestService");
        context.startService(serviceIntent); 

   }

首先我创建了BroadcastReceiver。我在清单文件中注册了这个接收器,如下所示:

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

在接收器中,我调用了以下服务:

public class TestService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub

        System.out.println("**inside onCreate");
        super.onCreate();
        Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
        Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555")); 
        startActivity(call);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub

        System.out.println("**inside onDestroy");
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        System.out.println("**inside onStart");
        super.onStart(intent, startId);
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    }

}

当我在启动应用程序后尝试启动设备时,强制关闭。如何在安卓中做到这一点? 提前感谢

I am doing an application in which I want to call a number when that device boot completed. My code is like this:

 @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("**inside onRecevier");

        Intent serviceIntent = new Intent();
        serviceIntent.setAction("com.test.app.TestService");
        context.startService(serviceIntent); 

   }

First I created BroadcastReceiver. I registered this receiver in manifest file like this:

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

In a receiver I called the below service:

public class TestService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub

        System.out.println("**inside onCreate");
        super.onCreate();
        Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
        Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555")); 
        startActivity(call);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub

        System.out.println("**inside onDestroy");
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        System.out.println("**inside onStart");
        super.onStart(intent, startId);
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    }

}

when I tried to boot a device after booting the application getting force close. How to do this in android??
Thanx in advance

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

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

发布评论

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

评论(3

撩心不撩汉 2025-01-03 06:18:52

您需要在从服务启动活动之前添加 NEW_TASK 标志:

Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555"));
call.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(call);

解释了:

请注意,如果从 Activity 外部调用此方法
Context,那么Intent必须包含FLAG_ACTIVITY_NEW_TASK
发射标志。这是因为,无需从现有的
活动,没有可在其中放置新活动的现有任务
因此需要将其放置在自己单独的任务中。

此外,您必须持有权限:

正如 Waqas 提到的,您最好从 onReceive 启动服务,例如:

 Intent serviceIntent = new Intent(context, TestService.class);
 context.startService(serviceIntent); 

确保您已完成了我所说的所有操作,如果您仍然遇到问题,那么编辑您的问题并从强制关闭中粘贴 logcat 将会很有帮助。

You need to add the NEW_TASK flag before starting an activity from a service:

Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555"));
call.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(call);

This explains:

Note that if this method is being called from outside of an Activity
Context, then the Intent must include the FLAG_ACTIVITY_NEW_TASK
launch flag. This is because, without being started from an existing
Activity, there is no existing task in which to place the new activity
and thus it needs to be placed in its own separate task.

Also, you must hold the permissions:

And as Waqas mentioned, it would be better for you to start your service from your onReceive like:

 Intent serviceIntent = new Intent(context, TestService.class);
 context.startService(serviceIntent); 

Make sure you've done all that I've stated, and if you continue to have an issue, then it would be helpful if you edit your question and paste the logcat from the force close.

傲娇萝莉攻 2025-01-03 06:18:52

将你的 onReceive 更改为此

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("**inside onRecevier");

        Intent serviceIntent = new Intent(context, TestService.class);
        context.startService(serviceIntent); 

   }

change your onReceive to this

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("**inside onRecevier");

        Intent serviceIntent = new Intent(context, TestService.class);
        context.startService(serviceIntent); 

   }
无戏配角 2025-01-03 06:18:52

强制关闭的原因有多种。辨别哪个的最好方法是查看日志。它应该准确地告诉您抛出了什么异常,并提示您如何解决问题。

There's a variety of reasons why you'd get a Force Close. The best way to tell which is to look at the log. It should tell you exactly what exception is being thrown, and give you a hint as to how to solve the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文