2013-10-23 53 views
1

如何在应用程序被终止并且服务保持活动状态时从广播接收器启动服务。 我的广播接收器:从brodcast接收器启动android服务

public class NetworkChangeReceiver extends BroadcastReceiver{ 

@Override 
public void onReceive(Context context, Intent intent) { 
    final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
    Intent intent1 = new Intent(context, ImageUploader.class); 
    if (wifi != null && wifi.isAvailable()) { 
     context.startService(intent1); 
     Toast.makeText(context, "start service execute", Toast.LENGTH_SHORT).show(); 
    } else if(mobile != null && mobile.isAvailable()) { 
     context.startService(intent1); 
    } 
} 

}

当应用程序被启动,所有做工精细。但是,当我杀了应用程序,我只看到敬酒,服务正在睡觉。请帮帮我!

回答

2

从 “SirKnigget” 的答案Here

它可能是从

startService(...)

变化

startForeground(。 ..)

而且还小心退出由

System.exit应用(0)

的服务将停止。因此,使用

完成()

从应用程序中退出。

您可以找到http://developer.android.com/reference/android/app/Service.html

希望这有助于为您解决问题的其他信息。