Activity 正在使用广播接收器打开多次

Activity is opening multiple times using broadcast receiver

我正在开发 android 作为电池指示器的应用程序,当电池电量不足或充满时打开 activity。

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

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

这里是主要内容Activity代码:

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();
}}

这是我的服务代码: 我认为我在开始时做错了什么 Activity。 在第

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);

    }
    }}

And this is my last "Last.cs" activity which is opening multiple time. but it works fine when i call finish() into Main Activity.

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();
    }
});

}  }

在 Manifest

中将你最后的 activity launchMode 设置为 singleTask
 <activity
      android:name=".Last"
      android:configChanges="orientation|screenSize"
      android:launchMode="singleTask"
      >
  </activity>