Answer the question
In order to leave comments, you need to log in
Why does it say "AppName has stopped"?
I wanted to make an application that will turn on the sound on the phone at 15:45, and turn off the sound at 8:45. It would be desirable to implement through AsyncTask.
Launching the application -> displays the time -> 5 seconds passes -> crashes. Why?
MainActivity.java
package com.pepelac.test_sound_change;
import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
assert am != null;
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT); /* mute */
MyTask longTask;
longTask = new MyTask();
longTask.execute();
}
@SuppressLint("StaticFieldLeak")
class MyTask extends AsyncTask<Void, Void, Void> {
@SuppressLint("ShowToast")
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast toast = Toast.makeText(getApplicationContext(),
"ok!", Toast.LENGTH_SHORT);
toast.show();
}
@Override
protected Void doInBackground(Void... params) {
while (true) {
task();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
public void task() {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int PM = calendar.get(Calendar.AM_PM);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(String.valueOf(hour) +':'+String.valueOf(minute));
if (hour == 8 && minute == 45 && PM == 0)
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
if (PM == 1) {
switch (dayOfWeek) {
case 2:
if (hour == 3 && minute == 45) {
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
case 3:
if (hour == 3 && minute == 45) {
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
case 4:
if (hour == 3 && minute == 45) {
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
case 5:
if (hour == 3 && minute == 45) {
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
case 6:
if (hour == 3 && minute == 45) {
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
case 7:
if ((hour == 1) && (minute == 0))
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question