简体   繁体   English

我是Android新手。 我希望仅在服务启动时使用android服务来阻止通话

[英]I am new to android. I wish to use android service for blocking call only when the service is started

This is the code should be run only when the service is running. 这是仅在服务运行时才应运行的代码。 And the service should be stopped in some other part. 并且该服务应在其他部分停止。

public void onReceive(Context context, Intent intent)
{

    this.context=context;
    this.intent=intent;

    if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
        return;


    else
    {
            number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

            if (Callblockactivity.blockList.contains(new Blacklist(number))) {
                System.out.println("i");
            }

            else {

                    disconnectPhoneItelephony(context);
                    return;

            }
       }
    }

Now, the service should be started in mainactivity as follow. 现在,应按如下所示在mainactivity中启动该服务。

 if (speed >10) {
  //service should be started.
  }

     else {
  //service should be stopped
   }

Please help with this. 请帮忙。

use startService to start a Service. 使用startService启动服务。 Use stopSelf or stopService to stop your service. 使用stopSelfstopService停止服务。

And you may use a static boolean variable inside your service in order to keep the flag whether the service is running. 而且,您可以在服务内部使用静态布尔变量,以保留该服务是否正在运行的标志。 Instead of that variable also you may dynamically register a receiver when you start your service, and unregister it when you stop the service. 除了此变量,您还可以在启动服务时动态注册一个接收器,并在停止服务时注销它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我只想在android中单击新图片时启动服务。 我该怎么做? - I want to start a service only when a new picture is clicked in android. How do I go about doing that? 当我打电话时重新创建Android中的服务 - The service in Android is recreated when I call 安卓 将服务或应用程序类用于locationListener - Android. Use service or application class for locationListener 我有在Android中发布的服务。 如何在iOS中浏览? - I have a service that is published in Android. How to browser it in iOS? 何时真正启动android服务? - When an android service is really started? 如何仅使用Android服务一次调用函数? - How can I call function 1 time only with Android service? 如果我关闭启动它的应用程序,我可以停止 Android Studio 中的服务吗? - Can i stop a service in Android Studio if I close the application that started it? 我在android中定义动作栏。 如何为每个操作添加新操作和点击侦听器? - I am defining actionbars in android. How do I add new actions and on click listener to each action? Android:服务销毁(onDestroy)时如何调用活动? - Android: How do I call an activity when service is destroyed (onDestroy)? 当我从android服务调用类时出现NullPointerException - NullPointerException when i call a class from an android service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM