我怎样才能运行一段代码,比方说,每1秒???

这是我的代码片段:
主要活动:

 private Context ctx;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn = (Button)findViewById(R.id.button1);
            ctx = this;
            btn.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                      AlarmManager am  = (AlarmManager)getSystemService(ALARM_SERVICE);
                      Calendar cal = Calendar.getInstance();
                      int inter =  1000;
                      Intent i = new Intent(ctx, Brodcast.class);
                      PendingIntent pi = PendingIntent.getBroadcast(ctx, 0, i, 0);
                      am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), inter, pi);
                }

            });
        }


广播级:

public class Brodcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(arg0, "This wil appear every one seconde", Toast.LENGTH_LONG).show();
        Log.e("this will apear every one seconde" , "this will apear every one seconde");
    }

}


但是没有显示日志或Toast的原因???请帮忙。

最佳答案

请确保已在清单文件中定义了广播接收器,或使用注释中建议的代码注册了该接收器。

关于java - 待定 Intent 并设置警报重复,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19428845/

10-09 19:13