使用广播接收器多次打开活动

时间:2016-05-02 11:23:11

标签: java android android-activity broadcast android-broadcastreceiver

我正在开发Android应用程序,它可用作电池指示灯,当电池电量低或满时打开活动。

当我在服务开始后从onCreate事件调用Main活动中的finish()时,它工作正常。

但是当我评论完成方法时,它会多次打开活动,我从BroadCast接收器打开。

以下是MainActivity Code:

public class Main extends Activity {
private MyService service;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

if (service == null) {

    Intent i = new Intent(this, MyService.class);
    startService(i);
}

finish();
}}

这是我的服务代码: 当我开始活动时,我想我做错了什么。 在行

getApplication().startActivity(intent);

完整的服务代码是:

public class MyService extends Service{

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("MyService", "onStartCommand");
    // do not receive all available system information (it is a filter!)
    final IntentFilter battChangeFilter = new IntentFilter(
            Intent.ACTION_BATTERY_CHANGED);
    // register our receiver
    this.registerReceiver(this.batteryChangeReceiver, battChangeFilter);
    return super.onStartCommand(intent, flags, startId);
}
private final BroadcastReceiver batteryChangeReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        checkBatteryLevel(intent);
    }
};

private void checkBatteryLevel(Intent batteryChangeIntent) {
    // some calculations
    final int currLevel = batteryChangeIntent.getIntExtra(
            BatteryManager.EXTRA_LEVEL, -1);
    final int maxLevel = batteryChangeIntent.getIntExtra(
            BatteryManager.EXTRA_SCALE, -1);
    final int percentage = (int) Math.round((currLevel * 100.0) / maxLevel);

    if(percentage==100)
    {
        Intent intent = new Intent(getBaseContext(), Last.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(intent);

    }
    if(percentage==15)
    {
        Intent intent = new Intent(getBaseContext(), Last.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(intent);

    }
    }}
  

这是我的最后一次" Last.cs"多次开放的活动。   但是当我将完成()调用到主要活动时,它工作正常。

public class Last extends Activity {
Button btnCancel;
Uri notification;
Ringtone r;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last);

notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();


 btnCancel = (Button) findViewById(R.id.stopsound);

 btnCancel.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

         r.stop();
    }
});

}  }

1 个答案:

答案 0 :(得分:3)

将您的上一个活动launchMode设置为Manifest中的 singleTask

 <activity
      android:name=".Last"
      android:configChanges="orientation|screenSize"
      android:launchMode="singleTask"
      >
  </activity>