2012-09-21 110 views
1

当我Force Close一个应用程序,并在BroadcastReceiver保持侦听意图?Android的 - 广播接收器不工作

我有这个BroadcastReceiver类:

public class Receiver extends BroadcastReceiver implements Variables { 

    CheckConexion cc; 

    @Override 
    public void onReceive(Context contxt, Intent intent) { 

     // Cuando hay un evento, lo diferenciamos y hacemos una acción. 

     if (intent.getAction().equals(SMS_RECEIVED)) { 
      Sms sms = new Sms(null, contxt); 
      sms.uploadNewSms(intent); 
     } else if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) { 
      // st.batterylow(contxt); 
     } else if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { 
      /* 
      * try { new PhoneState(contxt).battery(intent.getIntExtra("level", 
      * 0)); } catch (JSONException e) { e.printStackTrace(); } 
      */// Nothing at the moment 
     } else if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) { 
      Log.i("******", "Battery on"); 
     } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) { 
      // st.power(0, contxt); 
     } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED) 
       || intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED) 
       || intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) { 
      Database db = new Database(contxt); 
      if (db.open().Preferences(4)) { 
       Uri data = intent.getData(); 
       new ListApps(contxt).import_app(intent, contxt, data, 
         intent.getAction()); 
      } 
      db.close(); 
     } else if (intent.getAction().equals(
       ConnectivityManager.CONNECTIVITY_ACTION)) { 
      cc = new CheckConexion(contxt); 
      if (cc.isOnline()) { 

       Database db = new Database(contxt); 
       db.open(); 
       if (db.move() == 1) { 
        new UploadOffline(contxt); 
       } 
       db.close(); 

      } 
     } 

    } 

    public void register(Context c) { 
     IntentFilter i = new IntentFilter(); 
     i.addAction(SMS_RECEIVED); 
     i.addAction(Intent.ACTION_BATTERY_LOW); 
     i.addAction(Intent.ACTION_POWER_CONNECTED); 
     i.addAction(Intent.ACTION_POWER_DISCONNECTED); 
     i.addAction(Intent.ACTION_CALL_BUTTON); 
     i.addAction(Intent.ACTION_CAMERA_BUTTON); 
     i.addAction(Intent.ACTION_BATTERY_CHANGED); 
     i.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 
     c.registerReceiver(this, i); 
     IntentFilter apps = new IntentFilter(); 
     apps.addAction(Intent.ACTION_PACKAGE_ADDED); 
     apps.addAction(Intent.ACTION_PACKAGE_CHANGED); 
     apps.addAction(Intent.ACTION_PACKAGE_REMOVED); 
     apps.addDataScheme("package"); 
     c.registerReceiver(this, apps); 
    } 

    public void unregister(Context c) { 
     c.unregisterReceiver(this); 
    } 
} 

我通话记录,然后我Force Close。我想保留BroadcastRecever工作和收听传入的意图。

谢谢。

回答

2

广播接收机不会强制关闭应用程式后工作。这是通过设计嵌入到操作系统中的。

+0

我可以实现服务? –

+0

这不是事实。如果您使用registerReceiver()注册接收方,则其生存时间与注册它的对象的生存时间相关。因此,如果你杀了应用程序,接收器将不再工作。服务不会工作。您应该在您的应用清单中注册您的接收器,使其按照您的描述工作。 –

+1

托比亚斯,这是不作为API版本12.真请参阅http://developer.android.com/about/versions/android-3.1.html,部分“关于停止应用程序启动控制”。如果应用程序是力停止,即使在清单中声明 BroadcastReceivers将无法正常工作。 – WindyB

0

填写您的接收器在你的manifest。然后它将被永久激活(如果你喜欢使用包管理器,你可以手动启用/禁用它)。