gpt4 book ai didi

java - 待定 Intent 并设置闹钟重复

转载 作者:行者123 更新时间:2023-12-01 13:59:17 26 4
gpt4 key购买 nike

我如何才能运行一段代码,比如说每 1 秒运行一次?

这是我的代码 fragment :
主要 Activity :

 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都没有显示,为什么???请帮忙。

最佳答案

请确保您已在 list 文件中定义了广播接收器,或者使用评论中建议的代码进行注册。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com