0

我想在预定义的时间执行两个代码块。我为此目的使用 AlarmManager。两个独立的 AlarmManager 和两个独立的待处理意图。当我按下屏幕上的按钮时,我设置了 2 个要触发的警报(第一个在 5 秒后触发,第二个在 19 秒后触发)。我的问题是两个警报,因此两个相应的代码块在 5 秒后同时触发。我更改了两个待处理意图的 ID,但我正面临这个问题。

以下是我的主要活动 java 类代码:

public class PerseusAndroid extends Activity implements OnClickListener,  OnItemClickListener {

PendingIntent  pi,pi2;
AlarmManager amon,amoff;
BroadcastReceiver br,br2;


BluetoothSocket m_btSck;                                    

public static final int idTab2FWD = Menu.FIRST + 1,

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.PreseusAndroid);
    setup();
    setup2();
}

private void setup()
{       
    br = new BroadcastReceiver() {

        @Override

        public void onReceive(Context c, Intent i) {

            if (m_btSck != null )

                try {

                    m_btSck.getOutputStream().write('1');
                    Toast.makeText(getBaseContext(), "Led is ON.. : )", Toast.LENGTH_LONG).show();
                } 
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        };


        registerReceiver(br, new IntentFilter("net.pocketmagic.perseus") );
        final int _id2 = (int) System.currentTimeMillis();

        pi = PendingIntent.getBroadcast( this,_id2 , new Intent ("net.pocketmagic.perseus"),PendingIntent.FLAG_CANCEL_CURRENT );

        amon = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));

}

private void setup2()
{     

    br2 = new BroadcastReceiver() {

        @Override

        public void onReceive(Context c1, Intent i1) {

             if (m_btSck != null )

                try {

                    m_btSck.getOutputStream().write('0');
                  Toast.makeText(getBaseContext(), "Led is Off.. : )", Toast.LENGTH_LONG).show();
            } 
                catch (IOException e) {
                    // TODO Auto-generated catch block
                e.printStackTrace();
            }

             }
        };

        registerReceiver(br2, new IntentFilter("net.pocketmagic.perseus") );
        final int _id = (int) System.currentTimeMillis();
        pi2 = PendingIntent.getBroadcast( this, _id, new Intent("net.pocketmagic.perseus"),PendingIntent.FLAG_ONE_SHOT );

        amoff = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
} 

@Override
public void onClick(View v) {
    int cmdId = v.getId();
    if (cmdId == idTab2FWD)
    {   
        amon.set( AlarmManager.ELAPSED_REALTIME_WAKEUP,  SystemClock.elapsedRealtime() +5000, pi );
        //amoff.set( AlarmManager.ELAPSED_REALTIME_WAKEUP,  SystemClock.elapsedRealtime() +19000, pi2);
    }

  }
 }
}
4

1 回答 1

0

您正在使用相同的意图/过滤器注册两个意图/接收器。这意味着它也会触发另一个接收器。只需使 Intent/IntentFilter 特定于每个操作,即代替"net.pocketmagic.perseus"use:"net.pocketmagic.perseus.ACTION_1""net.pocketmagic.perseus.ACTION_2".

于 2013-10-27T15:50:02.637 回答