[英]android repeating alam is not getting stopped when stop button is clicked
I am setting a repeated alarm in the Android, which fires the activity. 我在Android中设置了重复警报,从而触发了该活动。
This activity is having two buttons: "stop" and "snooze". 此活动有两个按钮:“停止”和“延后”。 When I click on "snooze" alarm is postponed to 10 minutes. 当我单击“打no”时,警报被推迟到10分钟。
But the alarm is in slider window, ie 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());
}
}
And my XML file is: 我的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>
As you can see I have implemented Process.killProcess(pid)
method. 如您所见,我已经实现了Process.killProcess(pid)
方法。 Still my alarm is getting saved into the navigation drawer. 我的警报仍然保存在导航抽屉中。 I want to remove the activity from everywhere. 我想从任何地方删除活动。
i am using same intent for 32 alarms so i can not put the id of the pending intent to the intent as putExtra. 我对32个警报使用了相同的意图,因此我不能将未决意图的ID放到意图中,如putExtra。
Hopefully this will give you an idea of how to cancel/stop the alarm: 希望这将使您了解如何取消/停止警报:
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..
}
I copied it from one of my projects and it works for me. 我从一个项目中复制了它,并且对我有用。
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. 这不会使该活动进入最近的活动,并且通话结束将导致该活动的结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.