how to change ringer mode in background(Vibrate or Silent) - android

I'm trying to write a program in which user can set time that will change ringer mode to vibrate or normal in background. When I set the start time it's working but at the end time it's not changing and device continuing vibrate mode.
I just want, at the start time phone would be in vibrate and at end time phone would go back into the silent mode.
MainActivity.java
public class MainActivity extends Activity {
private PendingIntent pendingIntent;
private AlarmManager alarmManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
Calendar calendar1 = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 4);
calendar.set(Calendar.MINUTE, 37);
calendar.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, (int)System.currentTimeMillis(), myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
calendar1.set(Calendar.HOUR_OF_DAY, 4);
calendar1.set(Calendar.MINUTE, 38);
calendar1.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent1 = new Intent(MainActivity.this, MyReceiver1.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, (int)System.currentTimeMillis(), myIntent1,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent);
}}
and these are related BroadcastReceiver class..
MyReceiver.java
public class MyReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
AudioManager audio = (AudioManager)arg0.getSystemService(Context.AUDIO_SERVICE);
switch(audio.getRingerMode()){
case AudioManager.RINGER_MODE_NORMAL :
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Log.d("MODE", "was normal");
Log.d("MODE", "is vibrate");
break;
case AudioManager.RINGER_MODE_SILENT :
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Log.d("MODE", "was silent");
Log.d("MODE", "is vibrate");
break;
}
}}
MyReceiver1.java
public class MyReceiver1 extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
AudioManager audio=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
// TODO Auto-generated method stub
switch(audio.getRingerMode()){
case AudioManager.RINGER_MODE_NORMAL :
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Log.d("MODE", "was normal");
Log.d("MODE", "is silent");
break;
}
case AudioManager.RINGER_MODE_VIBRATE :
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Log.d("MODE", "was vibrate");
Log.d("MODE", "is silent");
break;
}
}}
and I declared
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver1">
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>
in my manifest file.
Please help me to solve this issue.
Thanks.

Dont't use both the receiver (MyReceiver and Myreceiver1) use single MyReceiver.
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>

Related

Make alarm Clock with BroadcastReceiver

I want to make a alarm clock in android and I write this with BroadcastReceiver But I have some problem with that...
I want to start alarm in specified time for example I want start alarm in every 3:50...
but when I am running the App alarming starting too...
There is my code bellow:
Any one could help me:
there is my broadcast Receiver:
public class MyBroadcastReceiver extends BroadcastReceiver {
public static MediaPlayer mediaPlayer;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.tester.alarmmanager")) {
Toast.makeText(context, "Its time!!!", Toast.LENGTH_SHORT).show();
mediaPlayer = MediaPlayer.create(context, R.raw.entezar);
mediaPlayer.start();
}
}
}
And in MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StartAlarm(03,50);
}
public void StartAlarm(int hour, int min){
Intent intent = new Intent(this, MyBroadcastReceiver.class);
intent.setAction("com.tester.alarmmanager");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),23433,intent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
Log.v("time picker",calendar.getTime()+"");
calendar.set(Calendar.HOUR_OF_DAY,hour);
calendar.set(Calendar.MINUTE,min);
calendar.set(Calendar.SECOND,00);
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT){
alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
Toast.makeText(MainActivity.this, "Alarm>kitkat", Toast.LENGTH_SHORT).show();
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
Toast.makeText(MainActivity.this, "Alarm<kitkat", Toast.LENGTH_SHORT).show();
}
}
And in Manifest:
<receiver android:name=".Utility.MyBroadcastReceiver">
<intent-filter>
<action android:name="com.tester.alarmmanager"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
add after calendar.getInstance()
calendar.setTimeInMillis(System.currentTimeMillis())
or check code under this link https://github.com/mrkostua/SmartAlarm/blob/master/app/src/main/java/com/mrkostua/mathalarm/alarms/mathAlarm/AlarmManagerHelper.kt

How to start service after reboot device in Android

I want start my Service after reboot device, I write below codes but when reboot device not start Service.
Manifest:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".alarm1.alarm1"></activity>
<receiver android:name=".alarm1.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<!-- Will not be called unless the application explicitly enables it -->
<receiver android:name=".alarm1.DeviceBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Activity code:
public class alarm1 extends AppCompatActivity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm1);
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);
findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
start();
}
});
findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAt10();
}
});
}
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancel() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
public void startAt10() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 20;
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 15);
Toast.makeText(alarm1.this, "Start at 17:11", Toast.LENGTH_SHORT).show();
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 3, pendingIntent);
}
}
AlarmReceiver codes:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}
DeviceBootReceiver codes:
public class DeviceBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
/* Setting the alarm here */
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show();
}
}
}
How can I fix this and when reboot device start Service?
Add permissions in Manifest.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Add receiver entry in Manifest.
<receiver android:enabled="true" android:name=".alarm1.DeviceBootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
You just need to
Request a permission
and
Explicitly enable the receiver.
The permission goes in the manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Then when the user sets an alarm (for example your start method), enable the boot receiver:
ComponentName receiver = new ComponentName(context, DeviceBootReceiver.class);
getPackageManager().setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
When the user cancels all alarms, you should disable the boot receiver using a similar piece of code.

Android alarm manager after boot

My alarmmanager code working good. But after boot I can't reschedule my alarm. Can you please tell me where I did mistake?
As you can see in DeviceBootReciever I put the toast. But even this toast doesn't appear. It means I can't listen boot completed.
But I didn't understand the reason. Here I have mentioned my code. Any help will be appreciated!
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent alarmIntent = new Intent(MainActivity.this,AlarmReceiver.class);
pendingIntent= PendingIntent.getBroadcast(MainActivity.this,0,alarmIntent,0);
findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
start();
}
});
findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAt10();
}
});
}
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 8000; // tekrar süresi milisaniye cinsinden
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancel() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
public void startAt10() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 8000; // tekrar süresi milisaniye cinsinden
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int hour = 14;
int min= 43;
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
AlarmReciever.class:
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
Toast.makeText(context,"AlarmReceiver Calisiyor",Toast.LENGTH_SHORT).show();
}
}
DeviceBootreciever:
public class DeviceBootReceiver extends BroadcastReceiver {
public void onReceive(Context context,Intent intent){
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
/* Setting the alarm here */
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show();
}
}
}
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Will not be called unless the application explicitly enables it -->
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".DeviceBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>

Creating a repeating notification with a receiver ( BroadcastReceiver )

I have created a receiver that Extends BroadcastReceiver. This is used to execute my Notification that repeats daily, based on the time selected by the user. For some reason i cant get this to work. I am worried that i am getting the time from the number pickers wrong, but i am new to programing and could use some help. Thank you in advance. Let me know if you see any Errors.
Here is my main activity (MyActivity)
public class MyActivity extends Activity {
TimePicker timePicker;
Button setAlarm;
private int hour;
private int minute;
PendingIntent pendingIntent;
int AM_PM;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
timePicker = (TimePicker) findViewById(R.id.timePicker);
setAlarm = (Button) findViewById(R.id.setUpAlarm);
setAlarm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setAlarm();
}
});
}
private void setAlarm() {
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
long time = 60 * hour + minute;
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, NotifyService.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
//create alarms
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
// AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
Here is my Notification class ( extends BroadcatsReceiver )
public class NotifyService extends BroadcastReceiver {
public NotifyService() {
}
#Override
public void onReceive(Context context, Intent intent) {
//generate notification // should be splash activity
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(null)
.setContentText("Your tip for today is ready")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.build();
}
// dont forget to compile "com.android.support:support-v4:18.0.+"
}
Here is my manifest
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".NotifyService"
android:enabled="true"
android:exported="true" >
</receiver>
</application>
Dont use a receiver with a filter in your android manifest. Create an 'explicit intent' instead of using an intent filter. This way,
Intent i = new Intent(your random intent Action name).
This should fix you problem

My AlarmManager for service update doesn't work

Why my AlarmManager for service update doesn't work? this is my code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService();
}
// Method to start the service
public void startService() {
startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService() {
stopService(new Intent(getBaseContext(), MyService.class));
}
Service class:
public class MyService extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
#Override
public void onCreate() {
poruka();
super.onCreate();
}
public void poruka(){
Toast.makeText(this, "OnCreate work!", Toast.LENGTH_LONG).show();
}
}
BroadcastReceiver and AlarmManager:
public class ServiceBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MyService.class));
Intent alarmIntent = new Intent(context, ServiceBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis(), 30*1000, pendingIntent);
}
}
And Permissions:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="me.example.nservice.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" android:enabled="true"
android:label="Servisv1" />
<receiver android:name="me.example.rqservice.ServiceBroadcast" android:process=":remote">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
I receive message "OnCreate work!" and "Service Started" only first time when i open app. Why update doesn't work every 30 sec?
To start a service every 30 seconds :
Inside onCreate() :
Here MyService is the name of the Service.
Intent myService = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(
this, 0, myService, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 30*1000,pendingIntent);
This'll start your service every 30 seconds. However, if the service is already running, which might be the case once it has been started the first time, from next time onwards the call will directly go to onStartCommand() method of the Service and not onCreate().
This is all that you need to do. However, if you want to make sure that the AlarmManager keeps restarting the service even after a phone is restarted, you'll need to add a BroadcastReceiver for Boot.

Categories

Resources