簡體   English   中英

我是Android新手。 我希望僅在服務啟動時使用android服務來阻止通話

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

這是僅在服務運行時才應運行的代碼。 並且該服務應在其他部分停止。

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;

            }
       }
    }

現在,應按如下所示在mainactivity中啟動該服務。

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

     else {
  //service should be stopped
   }

請幫忙。

使用startService啟動服務。 使用stopSelfstopService停止服務。

而且,您可以在服務內部使用靜態布爾變量,以保留該服務是否正在運行的標志。 除了此變量,您還可以在啟動服務時動態注冊一個接收器,並在停止服務時注銷它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM