【问题标题】:starting BroadcastReceiver or IntentService from non-activity class从非活动类启动 BroadcastReceiver 或 IntentService
【发布时间】:2015-02-12 20:01:55
【问题描述】:

如何从 non-activity 类启动 BroadcastReceiver 或 IntentService

我的意思是发送意图并运行 BroadcastService 或 IntentService

即:

我有课:

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 谢谢

【问题讨论】:

    标签: android class broadcastreceiver android-intentservice


    【解决方案1】:

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

    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;
        }
    }
    

    您的班级将如下所示。

    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
    } 
    

    但不是很好的解决方案。

    最后你可以创建回调监听器并在你的类中设置它,如下所示:

    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();
        }
    } 
    

    还有一些你有上下文的地方——例如在活动中:

    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
    } 
    

    【讨论】:

    • 对不起,我忘了说functionOne是@Override,所以我不能添加其他参数
    • 所以第一个解决方案是错误的 - 你必须使用第二个解决方案
    • 这是我所知道的所有解决方案:)
    • 可能我没看懂,不过我说public int functionOne()@Override,改成public int functionOne(Context context)就不是@Override
    • 如果这个函数被覆盖,你不能这样做,你不能添加新的参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    相关资源
    最近更新 更多