Android 从 ram 清除时广播不执行
Android Broadcast not execute when clear from ram
您好,我创建了广播接收器,它在 Intent.ACTION_BATTERY_CHANGED 事件中接收电池电量。它工作得很好但是当我使用从按住主页按钮滑动时从 ram 中删除这个应用程序然后它没有收到事件。
我的广播代码是
private BroadcastReceiver BatteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
Toast.makeText(BatteryIndicatorService.this,""+level, Toast.LENGTH_SHORT).show();
}
};
这可以在您的广播 运行 在后台进行时完成,您需要创建 service.In 此服务,您必须定义广播。
BatteryIndicatorService.java
public class BatteryIndicatorService extends Service {
private BroadcastReceiver BatteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
Toast.makeText(BatteryIndicatorService.this,""+level, Toast.LENGTH_SHORT).show();
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
// Register Receiver.
registerReceiver(BatteryReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
}
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
然后像
一样从 MainActivity 启动它
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start service in Background
startService(new Intent(this, BatteryIndicatorService.class));
}
在应用程序标签 Manifest.xml 中定义服务
manifest.xml
<service
android:name="com.ittl.batteryindicator.BatteryIndicatorService"
android:enabled="true" >
</service>
您好,我创建了广播接收器,它在 Intent.ACTION_BATTERY_CHANGED 事件中接收电池电量。它工作得很好但是当我使用从按住主页按钮滑动时从 ram 中删除这个应用程序然后它没有收到事件。 我的广播代码是
private BroadcastReceiver BatteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
Toast.makeText(BatteryIndicatorService.this,""+level, Toast.LENGTH_SHORT).show();
}
};
这可以在您的广播 运行 在后台进行时完成,您需要创建 service.In 此服务,您必须定义广播。
BatteryIndicatorService.java
public class BatteryIndicatorService extends Service {
private BroadcastReceiver BatteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
Toast.makeText(BatteryIndicatorService.this,""+level, Toast.LENGTH_SHORT).show();
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
// Register Receiver.
registerReceiver(BatteryReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
}
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
然后像
一样从 MainActivity 启动它MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start service in Background
startService(new Intent(this, BatteryIndicatorService.class));
}
在应用程序标签 Manifest.xml 中定义服务
manifest.xml
<service
android:name="com.ittl.batteryindicator.BatteryIndicatorService"
android:enabled="true" >
</service>