如何在android中的特定时间播放mp3文件?

时间:2015-03-01 20:49:40

标签: android

我是Android开发的新手。我的应用程序根据城市的经度和纬度计算祷告时间,并将它们保存在“fajr”,“dhuhr”,“maghrib”变量中。 现在我想在那段时间播放一个mp3文件,即使我的应用程序已关闭,但我不知道如何使用它的服务。

public class ShowTimeActivity extends Activity {

double lat=0;
double lng=0;
double fajr;
double sunrise;
double dhuhr;
double sunset;
double maghrib;
double midnight;
boolean one=false;
boolean one1=false;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show_time);




    final LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    final String provider = LocationManager.GPS_PROVIDER;
    final Location location = 
      locationManager.getLastKnownLocation(provider);

    final String provider2 = LocationManager.NETWORK_PROVIDER;
    final Location location2 = 
      locationManager.getLastKnownLocation(provider2);



    Intent in= getIntent();
    Integer data2 =in.getIntExtra("key2", 0);


    TextView fajr_time=(TextView) findViewById(R.id.textView9);
    TextView sunrise_time=(TextView) findViewById(R.id.textView10);
    TextView dhuhr_time=(TextView) findViewById(R.id.textView11);
    TextView sunset_time=(TextView) findViewById(R.id.textView13);
    TextView maghrib_time=(TextView) findViewById(R.id.textView14);
    TextView midnight_time=(TextView) findViewById(R.id.textView16);

    String[] latitude=getResources().getStringArray(R.array.Latitude);
    String[] longetude=getResources().getStringArray(R.array.Longtude);
    lat=Float.valueOf(latitude[data2]);
    lng=Float.valueOf(longetude[data2]);

    Time today = new Time(Time.getCurrentTimezone());
    today.setToNow();
    int year=today.year;
    int month=today.month+1;
    int day=today.monthDay;


    final pray_time pt=new pray_time() ;
    int JD=pt.julianDate(year, month, day);
    pt.pray_time(JD);




    dhuhr=12+3.50-(lng/15)-pt.get_Eqt();
    sunrise=dhuhr-pt.cal_time(0.833f,lat);
    sunset=dhuhr+pt.cal_time(0.833f,lat);
    fajr=dhuhr-pt.cal_time(17.7f,lat);
    maghrib=dhuhr+pt.cal_time(4f,lat);
    midnight=(sunset+fajr)/2+12;


    fajr_time.setText(pt.floatToTime24(fajr));
    sunrise_time.setText(pt.floatToTime24(sunrise));
    dhuhr_time.setText(pt.floatToTime24(dhuhr));
    sunset_time.setText(pt.floatToTime24(sunset));
    maghrib_time.setText(pt.floatToTime24(maghrib));
    midnight_time.setText(pt.floatToTime24(midnight));

    Button show_gheble =(Button) findViewById(R.id.button1);
    show_gheble.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent1 = new Intent(ShowTimeActivity.this,Show_gheble.class);

            if(!one)
            {
                if (locationManager.isProviderEnabled(provider)) 
                {
                    startActivity(intent1);
                }
                else
                {
                    showSettingsAlert("GPS");
                }
                one=true;
            }
            else if(!one1)
            {
                if(locationManager.isProviderEnabled(provider2))
                {
                    startActivity(intent1);
                }
                else
                {
                showSettingsAlert("NETWORK");
                }
                one1=true;
            }
            else {
                startActivity(intent1);
            }

        }
    });

1 个答案:

答案 0 :(得分:0)

要获取当前时间/日期:Get current time and date on Android,请将它们与您的祈祷时间进行比较。

在特定时间播放您的mp3文件,您可以使用' AlarmManager'与' BroadcastReceiver' :http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/然后添加' MediaPlayer'对你的活动:

MediaPlayer mp;
mp = MediaPlayer.create(getApplicationContext(), R.raw.yourmp3file);
mp.setLooping(true);
mp.prepare();
mp.start();

希望这有帮助!