如何 运行 我的服务在后台运行并在 Android 中关闭 Broadcast Receiver?

How do I run my serivice on background and close BroadcastReceiver in Android?

我想 运行 我的短信服务后台 reader。 后台服务已 运行ning。但是我不想看到App的界面,为此想到了一个方法。我隐藏了 activity_main.xml,并且只想在我的 BroadcastReceiver 完成任务时关闭(运行 在后台),但没有找到执行此操作的方法。 (顺便说一句,我的 MainActivity class 是空的)。

这里是 MyReceiver class 和 BroadcastReceiver:

public class MyReceiver extends BroadcastReceiver  {

    public static final String SMS_EXTRA_NAME = "pdus";

    public void onReceive(Context arg0, Intent arg1)
    {
       Intent intent = new Intent(arg0,MyService.class);
        arg0.startService(intent);


        String messages = "";
        Bundle extras = arg1.getExtras() ;
        if ( extras != null )
        {
            // Get received SMS array
            Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );

            if ( smsExtra != null) {

                for (int i = 0; i < smsExtra.length; ++i) {
                    SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);

                    String body = sms.getMessageBody().toString();
                    String address = sms.getOriginatingAddress();

                    messages += "SMS from " + address + " :\n";
                    messages += body + "\n";

                    // Here you can add any your code to work with incoming SMS
                    // I added encrypting of all received SMS
                }

                // Display SMS message
                Toast.makeText(arg0, messages, Toast.LENGTH_SHORT).show();
            }


        }
    }
}

这里是MyService.java

public class MyService extends Service {

    @Override
    public void onStart(Intent intent, int startid) {
        Intent intents = new Intent(getBaseContext(), MainActivity.class);
        intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intents);
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();

    }

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

我终于从这里来了How to unregister BroadcastReceiver但是我无法运行他们的解决方案。

将接收器注册为服务初始化的一部分,而不是在清单中。这将允许您的接收器绑定到该服务,并在服务存在时(并且 只要)继续。

MyService

public class MyService
extends Service
{
    protected MyReceiver m_rcv = null ;

    @Override
    public void onCreate()
    {
        super.onCreate() ;
        m_rcv = (new myReceiver()).bindToContext(this) ;
    }

    @Override
    public void onDestroy()
    {
        this.unregisterReceiver( m_rcv ) ;
        super.onDestroy() ;
    }

    // rest of your code goes here
}

MyReceiver

public class MyReceiver
extends BroadcastReceiver
{
    protected Context m_ctx = null ;

    public MyReceiver bindToContext( Context ctx )
    {
        m_ctx = ctx ;
        IntentFilter filter = new IntentFilter() ;
        filter.addAction( /* Whatever action you need to intercept. */ ) ;
        // Continue adding all desired actions to the filter...
        ctx.registerReceiver( this, filter ) ;
        return this ;
    }

    // rest of your code goes here
}