2015-09-15 44 views
0

我想在短信阅读器的背景上运行我的服务。 服务已在后台运行。但我不想看App的界面,认为这是一种方法。我隐藏了activity_main.xml,并且只是想在执行任务时关闭(在后台运行)我的BroadcastReceiver,并且没有找到办法做到这一点。 (顺便说一下,我的MainActivity类是空的)。如何在后台运行我的服务并在Android中关闭BroadcastReceiver?

这里是MyReceiver类广播接收器:

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出来,但我不能运行他们的解决方案。

+0

你想对你的BroadcastReceiver到不能再运行,或者你希望它在后台运行? '只是想关闭(运行在免费)我的BroadcastReceiver' – nukeforum

+0

对不起,错了。自动关闭应用程序界面并在免费运行BroadcastReceiver。 –

+0

我明白了。必须尝试将'BroadcastReceiver'放入'Service'中? – nukeforum

回答

0

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

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 
} 
相关问题