我想在特定時間每天更改我的鈴聲音量。我使用日曆來指定時間,並且我試圖使用alarmManager來執行它。這是我的。 (我是noobie去容易對我)android日程安排任務在特定時間每天執行
package com.example.ringervolume.app;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends ActionBarActivity {
private AudioManager audio;
private PendingIntent pendingIntentam;
private PendingIntent pentdingIntentpm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewTime = (TextView) findViewById (R.id.textTime);
audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
//ringer volume for the am.
Calendar calendaram = Calendar.getInstance();
calendaram.set(Calendar.HOUR_OF_DAY, 21);
calendaram.set(Calendar.MINUTE, 32);
calendaram.set(Calendar.SECOND, 0);
calendaram.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntentam = PendingIntent.getBroadcast(MainActivity.this, 0,myIntent,0);
AlarmManager alarmManageram = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManageram.set(AlarmManager.RTC, calendaram.getTimeInMillis(), pendingIntentam);
}
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent scheduledIntent = new Intent(context, myscheduleactivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
}
public class myscheduleactivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
是什麼你的代碼有錯? – Raghunandan
我忘了添加它,基本上它不會設置音量在我指定的時間振動。 – andyADD
您有另一個活動作爲當前活動的內部類。將它移動到一個單獨的'.java'文件。另請參閱是否已註冊廣播接收器。 – Raghunandan