点击停止按钮时,机器人重复阿拉姆是没有得到停止 [英] android repeating alam is not getting stopped when stop button is clicked

查看:165
本文介绍了点击停止按钮时,机器人重复阿拉姆是没有得到停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置在Android,这触发活动重复报警。

I am setting a repeated alarm in the Android, which fires the activity.

本活动有两个按钮:停止和暂停。当我点击打盹的报警被推迟到10分钟。

This activity is having two buttons: "stop" and "snooze". When I click on "snooze" alarm is postponed to 10 minutes.

但警报在滑块窗口,也就是说,它是onresume状态。
我要彻底停止报警两个按钮。

But the alarm is in slider window, i.e it is in onresume state.
I want to stop the alarm completely on both the buttons.

    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)方法。不过我报警是越来越保存到抽屉式导航。我想从任何地方除去活动。

As you can see I have implemented Process.killProcess(pid) method. Still my alarm is getting saved into the navigation drawer. I want to remove the activity from everywhere.

我使用相同的意向32个报警,所以我不能把挂起的意图ID的意图是putExtra。

i am using same intent for 32 alarms so i can not put the id of the pending intent to the intent as putExtra.

推荐答案

在活动中被解雇的menifest文件中的条目添加以下代码行。

add the following lines in the entry of the activity that is fired in the menifest file.

android:excludeFromRecents="true" 

这不会让活动进入最近通话,并呼吁结束将导致活动的结束。

this will not let the activity go into recents and calling finish will lead to end of the activity.

这篇关于点击停止按钮时,机器人重复阿拉姆是没有得到停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆