从非 activity class 启动 BroadcastReceiver 或 IntentService

starting BroadcastReceiver or IntentService from non-activity class

如何从非activity class

启动BroadcastReceiver或IntentService

开始我的意思是发送意图并制作 运行 BroadcastService 或 IntentService

即:

我有 class:

public class NumberOne{
     @Override
     public int functionOne(){
       int i = 1 + 4;

        if(/*something is true*/){
        Intent intent = new Intent(this,intetServiceOne.class);
        intent.putExtra("id","path");
        context.startService(intent);
      }
      else {/*continue*/
      }
        return i;  
      }
       //other functions
     } 

如果 functionOne 中的条件为真,则启动 IntentService

public class IntentServiceClassOne extends IntentService {
     public IntentServiceClassOne () {
        super("IntentServiceClassOne ");
     }

    @Override
    protected void onHandleIntent(Intent intent) {
    String data = intent.getStringExtra("id");
        Log.d("dataIs: ", data);
    }
    //more functions what to do
    }

它不依赖于它是 IntentService 还是 BroadcastReceiver 谢谢

启动服务您需要上下文实例。您可以将其作为构造函数参数传递:

public class NumberOne{
     Context context;

     public NumberOne(Context context){
         this.context = context;
     }
     public int functionOne(Context context){
       int i = 1 + 4;

        if(/*something is true*/){
           Intent intent = new Intent(this,intetServiceOne.class);
           intent.putExtra("id","path");
           context.startService(intent);
        }
        else {/*continue*/
        }
        return i;  
    }
    //other functions
} 

不用传Activity实例,Context就够了。它可以是应用程序上下文。可以通过getApplicationContext()方式获取

您还可以在 Application 对象中创建静态实例并从中获取它:

public class YourApplication extends Application {

    public static YourApplication INSTANCE;

    public void onCreate(){
        super.onCreate();

        INSTANCE = this;
    }
}

您的 class 将如下所示。

public class NumberOne{

     public int functionOne(Context context){
       int i = 1 + 4;

        if(/*something is true*/){
           Intent intent = new Intent(this,intetServiceOne.class);
           intent.putExtra("id","path");
           YourApplication.INSTANCE.startService(intent);
        }
        else {/*continue*/
        }
        return i;  
    }
    //other functions
} 

但不是很好的解决方案。

最后您可以创建回调侦听器并将其设置在您的 class 中,如下所示:

public class NumberOne{
     //add setter
     YourListener yourListener;
     public int functionOne(Context context){
       int i = 1 + 4;

        if(/*something is true*/){
           if(yourListener != null){
               yourListener.onFunctionOneCall();
           }
        }
        else {/*continue*/
        }
        return i;  
    }
    //other functions

    public interface YourListener{
        void onFunctionOneCall();
    }
} 

还有一些你有上下文的地方 - 例如 activity:

numberOneInstance.setYourListener(new YourListener(){
    @Override
    public void onFunctionOneCall(){
               Intent intent = new Intent(this,intetServiceOne.class);
               intent.putExtra("id","path");
               this.startService(intent);
    }
});

或者您可以通过 setter

设置上下文
public class NumberOne{
     Context context;

     public setContext(Context context){
         this.context = context;
     }
     public int functionOne(Context context){
       int i = 1 + 4;

        if(/*something is true*/){
           if(context != null){
              Intent intent = new Intent(this,intetServiceOne.class);
              intent.putExtra("id","path");
              context.startService(intent);
           }
        }
        else {/*continue*/
        }
        return i;  
    }
    //other functions
}