【问题标题】:android repeating alam is not getting stopped when stop button is clicked单击停止按钮时,android重复警报不会停止
【发布时间】:2015-04-19 20:05:00
【问题描述】:

我在 Android 中设置了一个重复警报,它会触发活动。

此活动有两个按钮:“停止”和“打盹”。当我点击“贪睡”时,闹钟会推迟到 10 分钟。

但警报在滑块窗口中,即处于 onresume 状态。
我想在两个按钮上完全停止闹钟。

    public class RingAlarm extends Activity {

    MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ring_alarm);
        Intent in;
        in = getIntent();

        String s=in.getStringExtra("habbit_id");
        int col= in.getIntExtra("habbit_Color",0);
        ScrollView sv = (ScrollView)findViewById(R.id.sv_ra);
        if(col<0)
        {
            sv.setBackgroundResource(R.color.red);
        }
        if(col>0) {
            sv.setBackgroundResource(R.color.green);
        }
        if(col==0)
        {
            sv.setBackgroundResource(R.color.yellow);
        }
        System.out.println("alarm string" + s);
        TextView tv = (TextView)findViewById(R.id.habbit_ra);
        tv.setText(s);
       //  mp = MediaPlayer.create(getApplicationContext(), R.raw.song1);
        //mp.start();

    }
    public void onthecancelringclicked(View view){
        //mp.stop();
        int id=Process.myPid();
        finish();
        Process.killProcess(id);
        //System.exit(0);

    }

    public void onthesnoozeringclicked(View view){
        //mp.stop();
        long l=System.currentTimeMillis();
        Intent i = this.getIntent();
        Random r = new Random();
        int ri =r.nextInt(1000000)+64;
        PendingIntent pi = PendingIntent.getActivity(this,ri,i,0 );
        AlarmManager am = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP,l+600000,pi);

        finish();
        Process.killProcess(Process.myPid());
    }
}

我的 XML 文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@color/background"
>

<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="@string/app_name"
    android:gravity="left|center"
    android:padding="7dp"
    android:background="@color/user_profile"/>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/sv_ra">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center"
            android:id="@+id/habbit_ra"
            android:text="habbit_name"/>

        <AnalogClock
            android:text="time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:layout_gravity="center"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="bsy your self"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:id="@+id/habbit_message_ra"/>

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="0"
        android:background="@color/user_profile">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="stop"
            android:background="@color/user_profile"
            android:onClick="onthecancelringclicked"/>
        <Button
            android:background="@color/user_profile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="snooze 10 min"
            android:onClick="onthesnoozeringclicked"/>

        </LinearLayout>

如您所见,我已经实现了Process.killProcess(pid) 方法。我的警报仍然保存到导航抽屉中。我想从任何地方删除活动。

我对 32 个警报使用相同的意图,因此我不能将待处理意图的 id 作为 putExtra 放入意图。

【问题讨论】:

  • 有什么理由不能使用developer.android.com/reference/android/app/…
  • 因为首先我不确定它是否会取消警报或终止活动,其次我不能将待处理意图的 id 放在 intent.putExtra 中,因为我对 32 个警报使用相同的意图。
  • 然后对其他警报使用不同的意图。我认为取消闹钟真的很简单,你不必发明一些复杂的方法来做到这一点。
  • 这不是实现事物的好方法。当您只需要一个意图时,为什么会创建多个对象导致内存浪费。相反,应该有一种方法可以从警报活动中获取待处理意图的 ID
  • sir am.cancel 只会从 AlarmManager 取消该警报。它不会杀死被触发的活动。

标签: java android


【解决方案1】:

希望这能让您了解如何取消/停止闹钟:

Intent intent = new Intent(context, MyClass.class);
PendingIntent recurring = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    if(active) { //If alarm is active
        alarms.cancel(recurring);
        recurring.cancel();
    } else {
        // Do something..
    }

我从我的一个项目中复制了它,它对我有用。

【讨论】:

  • 我不知道警报活动中待处理意图的 ID
  • 您创建一个并在取消警报时使用相同的一个。在上面的代码中,它是0,因此您可以将其更改为您在活动中使用的那个。
【解决方案2】:

在清单文件中触发的活动条目中添加以下行。

android:excludeFromRecents="true" 

这不会让活动进入最近,调用完成将导致活动结束。

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2015-04-25
    • 1970-01-01
    • 2020-04-11
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多