嗨,我是Android新手,我正在写闹钟。我有一个小问题,如何从其他类调用方法。
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener, OnCheckedChangeListener {
@Override
protected void onCreate(final Bundle savedInstanceState) {
final Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View w) {
Alarm_Reciver alarm = new Alarm_Reciver();
alarm.set...?
下一课
public class Alarm_Reciver extends BroadcastReceiver {
AlarmManager alarm_manager;
private PendingIntent pending_intent;
MediaPlayer mp;
@Override
public void onReceive(Context context, Intent intent) {
}
public void setAlarm(Context context, Intent intent) {
mp = MediaPlayer.create(context, R.raw.d_s_h);
mp.start();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
Toast.makeText(context, "Alarm !!!!", Toast.LENGTH_LONG).show();
wl.release();
ComponentName cn = new ComponentName(context, Alarm_Reciver.class);
PackageManager pac = context.getPackageManager();
pac.setComponentEnabledSetting(cn,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
alarm_manager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Intent i = new Intent(context, Alarm_Reciver.class);
pending_intent = PendingIntent.getBroadcast(context, 0, i, 0);
alarm_manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000, pending_intent);
Toast.makeText(context, "Alarm On", Toast.LENGTH_LONG).show();
}
}
public void cancelAlarm(Context context) {
Intent intent = new Intent(context, Alarm_Reciver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarManager.cancel(sender);
if (alarm_manager != null) {
alarm_manager.cancel(pending_intent);
Toast.makeText(context, "Alarm off", Toast.LENGTH_LONG).show();
}
ComponentName cn = new ComponentName(context, Alarm_Reciver.class);
PackageManager pac = context.getPackageManager();
pac.setComponentEnabledSetting(cn,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
}
}
1。
所以,我想在单击button2时调用方法setAlarm,我尝试了几次(使用上下文和意图,例如alarm.setAlarm(context, intent)
或alarm.setAlarm(this, context)
,它没有工作,我也想调用方法取消按钮关闭时报警,但据我所知,它与按钮上的相同。
2。 我的下一个问题是,它是正确写入的,如果警报代码出现在if()指令中,或者它应该只在if()之外的方法setAlarm()中?也许代码的某些部分应该在onReceive()方法中。我稍后会添加一些东西,但现在可以了吗?
@UPGRADE
Ad1.
我尝试在XML中添加setAlarm,但它引发了我:
Corresponding method handler '
public void SetAlarm(android.view.View)
' not found
所以我尝试添加我的XML(工具:忽略):OnClick就像这样:
public void SetAlarm(android.view.View)
而不是
tools:ignore="MissingConstraints,OnClick"
当我得到我的XML时
tools:ignore="MissingConstraints"
抛出:'MainActivity'中缺少方法'SetAlarm'或签名错误
我在另一个线程的stackoverflow上读过这个,有人写这个: 您是否使用该方法的正确签名?
tools:ignore="MissingConstraints,OnClick
我的方法没有View对象,所以我必须添加它?怎么做?
Ad2的。 它应该是:(如果声明在最后)
Methods defines using the onClick attribute must meet the following requirements:
must be public
must have a void return value
must have a View object as parameter (which is the view that was clicked)
like
public void addWord(View view) {
//your action
}
或者:
alarm_manager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Intent i = new Intent(context, Alarm_Reciver.class);
pending_intent = PendingIntent.getBroadcast(context, 0, i, 0);
alarm_manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 60 * 1000, pending_intent);
Toast.makeText(context, "Alarm On", Toast.LENGTH_LONG).show();
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
});
我的意思是在if()语句中应该是所有代码还是在if()语句之外?
感谢您的帮助,我希望我能说清楚。
答案 0 :(得分:0)
第一个问题 你可以通过添加来解决它 android:onClick:按钮的XML代码中的setAlarm
我真的不明白第二个问题