Unable to disable notification vibration using checkbox(Android) - android

I am using Notification in MainActivity.class and i have checkbox in another Settings.class from where i want to enable or disable vibration on notification. Everything working fine but when i uncheck the checkbox vibrate still remain until refresh or restart the app. I don't want to refresh the activity or restart the app.
In main class :
#Override
public void onResume() {
super.onResume();
if(settingsAct.checkStatus1==1){
notify.defaults |= notify.DEFAULT_VIBRATE;
}
}
In Settings class :
if(mCheckVibrate.isChecked()){
checkStatus1 = 1;
}else{
checkStatus1 = 0 ;
}

Finally i have solved my problem i have changed from default notification vibration to following:
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(300);
For disabling vibration i have used :
vibrator.cancel();

Related

history applications stack - still with last intent

I have a service that turn on my app sometimes, the main activity prompt Dialog message to user sometimes,
after the user answer YES\NO I call to finish() to close the app.
my problem is when the message is shown, user answers it and app was call to finish() and when you look in the recent history you played before (in samsung for example you press long on home button) you will see my app along with other apps user started.
when you push it to open it again the Dialog show again..
How to show the activity when launched from recent app without showing the Dialog
public void dialog_1(){
myDialogViewN = new MyDialogViewNegativeTime(MainActivity.this);
// Setting vibrator
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern= {100, 1000};
vibrator.vibrate( pattern,0 );
// Setting 2 Dialog Listeners
myDialogViewN.setOnDialogListener(new DialogListener() {
#Override
public void onNegativeClick()
{
// Stopping Vibraror
if (vibrator.hasVibrator()){
vibrator.cancel();
vibrator = null;
}
initialize_DialogToUser(); /// ??
SendDataToService(3); //doesn't want reminder
myDialogViewN.dismissDialog();
waitForDialogAnswer=false;
finish();
}
#Override
public void onPositiveClick()
{
// Stopping Vibrator
if (vibrator.hasVibrator()){
vibrator.cancel();
vibrator = null;
}
initialize_DialogToUser();
SendDataToService(1); //remind!
myDialogViewN.dismissDialog();
squre.setImageResource(R.drawable.triangle_red2);
waitForDialogAnswer=false;
finish();
} });
myDialogViewN.show();
}
#Override
public void onDestroy()
{
super.onDestroy();
// close/stop running application on background
int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
}
finally i find solution,
the problem was when you open the app from history - the last intent that comes from service to your activity stay there and not goes, in contrary for openning the app by click it's own icon. (different openning ways).
my solution:
send 2 intent's from service to activity,
the first - with what you really need.
the second - after you receive your answer in the service. in the second you will not put any data! it is a mere intent that comes to change the "stuck" intent in history-app open way.
maby it is stupid, but it's the only solution i found :)

Android 4.2.1 / 4.2.2 Notifications vibrate in silent mode

Prior to Android 4.2.1 the following code worked just fine
notification.audioStreamType = AudioManager.STREAM_RING;
notification.flags = notification.flags | Notification.FLAG_INSISTENT
| Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(ID_NOTIFICATION_SOUND, notification);
Vibration is done separately, depending on the state of a preference setting.
vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(vibr_pattern1, 0);
Now with Android 4.2.1+ there is always vibration even if the phone is set to silent (no vibration) and set to vibrate mode, even if the app settings for vibration are set to off.
Interestingly there is no vibration when the phone is in normal mode.
As mentioned, everything worked fine before 4.2.1.
Can someone help? What did google change here? Is it related to this bug report?
Regards,Andreas
I fixed it like this:
//vibration workaround for android 4.2.1+
notification.vibrate = new long[]{0,0};
//end workaround
I manually add "no vibration" and if the user chooses vibration it's done with an Vibrator object. If not, the code above keeps everything quiet. As mentioned before, this was not necessary before 4.2.1+
I know this is old. But for some one who is looking for answer. You can try this.
AudioManager audio = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
switch( audio.getRingerMode() ){
case AudioManager.RINGER_MODE_NORMAL:
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
v.vibrate(500);
} catch (Exception e) {
e.printStackTrace();
}
break;
case AudioManager.RINGER_MODE_SILENT:
break;
case AudioManager.RINGER_MODE_VIBRATE:
v.vibrate(500);
break;
}

How to override the system default value for the duration when LED will be on

I have written a small test app (borrowed examples from previous implementation given in Stack Overflow) that will turn on Notification LED on my tablet . When I test this app on the tablet its not working as desired , i.e the LED turns on for a very small amount of time (and not for the 5 seconds duration that I have mentioned in my program). I guess its taking the system default value for the duration for which the LED needs to be ON/OFF rather than taking the value specified in my app. Has anyone seen a similar problem earlier ? Is there a way for me to override this system specific value ? My code is as below :
public class SampleActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
RedFlashLight();
}
private void RedFlashLight()
{
NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
Notification notif = new Notification();
notif.ledARGB = Color.RED;
notif.flags = Notification.FLAG_SHOW_LIGHTS ;
notif.ledOnMS = 5000; //5 seconds
notif.ledOffMS = 0; //do not turn it off
nm.notify(10, notif);
}
}
Is there a way for me to override this system specific value ?
Beyond what you are already doing? No.
Bear in mind:
Not all devices have LEDs
Not all devices that do have LEDs will necessarily use them for notifications
Device manufacturers can otherwise ignore the requests in the Notification object, such as LED color
So, you can ask for whatever you want, and whether you get it will depend on the device.

how to cancel a vibration in alert box?

I'm creating a app that popup a alert box that have to vibrate until the user click ok or cancel
my app vibrate good when i open the alert box but when i click ok or cancel app crash
this is coding i used for creating a alert box with vibrate
Vibrator v;
button = (Button) findViewById(R.id.buttonAlert);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
vibration();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Your Title");
alertDialogBuilder.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
MainActivity.this.finish();
v.cancel();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
v.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
and this is a vibrate method
public void vibration()
{
Vibrator v = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
long[] pattern = { 0, 3000, 3000 };
v.vibrate(pattern, 0);
// v.vibrate(5000);
}
I am getting error when i used Vibrator.cancel(); or v.cancel(); can any one help me
Try using vibrate(3000); which is equivalent to let the device vibrate for 3 seconds, insted of trying to cancel a started service.
Try to cancel the vibrator before exiting from the Dialog. See Vibrator class for more details.
try like below.....
Vibrator v = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
long[] pattern = {0,1000,1000};
v.vibrate(pattern, -1);
set the alert flag from default to
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL
This way, the notification will not cause vibrate.
If you want to make it vibrate also, you will have to give permission in manifest file for it
Make sure that application seeks vibrate permission in manifest. The application crash is due to the reason that your application does not have permission to vibrate the phone.
Here is the code
<uses-permission android:name="android.permission.VIBRATE" />
Edit:
I think you what you need is something different. You need a way to stop vibration after starting it.
Vibration service runs in a different context and hence you might not be able to communicate with it directly. There are several ways to do that but I am suggesting another easy way.
Instead of running vibration continuously, try running it for a small interval and put a timer in your dialog that will fire the vibration on after some time.
This way, you will not have to stop the vibration. As soon as the dialog is dismissed, the timer will stop firing and vibrate will stop. You will not need to explicitly turn off the vibration.
in your code you have v.cancel(); but as i can see this variable is not initialized, you are using another variable inside the function vibration() try to cancel an initialized variable of vibrate .
use this function
public Vibrator vibration() {
Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
long[] pattern = { 0, 3000, 3000 };
v.vibrate(pattern, 0);
// v.vibrate(5000);
return v;
}
and start the vibration like that
final Vibrator v = vibration();
P.S. i know it is an old question but maybe someone want the answer.

Changing color of LED notification every 5 seconds

I'm trying to turn on the LED notification, for example the green color. Turn the screen off, and every 5 seconds change that color to red (green->red, red->green). I think I've done everything: in a service I created a timer which executes method to display notification.
public class LEDService extends Service
{
private boolean TimerStarted;
private Timer timer;
private NotificationManager myNotificationManager;
private long LastColor;
public TurnLedOn()
{
Notification notify = new Notification();
notify.flags |= Notification.FLAG_SHOW_LIGHTS;
notify.LedOnMS = 500;
notify.LedOffMS = 0;
//I in other example I also used array of colors
if (LastColor == 0x00FF00) notify.LedARGB = 0xFF0000; else notify.LedARGB = 0x00FF00;
LastColor = notify.LedARGB;
}
private MyTimerTask extends TimerTask
{
#Override
public void run()
{
TurnLedOn();
}
}
#Override
public void OnCreate()
{
TimerStarted = false;
myNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
if (TimerStarted == false)
{
timer = new Timer();
timer.schedule(new MyTimerTask(), 0, 5000);
}
return START_STICKY;
}
#Override
public IBinder onBind()
{
return null;
}
#Override
public void onDestroy()
{
timer.close();
}
}
What is the problem? The LED doesn't change its color... When I turn the screen off, the color is green until I turn on the screen and turn it off again. I want to start this service once, turn off the screen and see the LED light changing color :).
BTW: I tested my phone and it CAN show green and red lights so it's not the problem.
Thank in advance and sorry for my English.
I can't answer to my own question so I'll add this here:
Thanks for your suggestions Olsavage, I added clearAll() to my code, but effect is still the same ;/. I also added logging to my application (Log.i() ). It looks like the system is stopping my service when I turn the screen off (why??). It is something this way:
Screen Turned On:
The timer is running and notification is deployed (but I can't see led on, because to see it i have to turn the screen off :P )
Click on the lock button:
The timer is almost stopped, so the LED sometimes changes color once.
Screen is turned Off:
The timer is not working, TurnLedOn method is not running anymore. LED doesn't change colors.
Now the question is: why my service is stopped after turning the screen off? Even when I am doing simple operation in it (like incrementing a variable). Maybe I have to set its priority or something?
I changed the timer interval to 100ms and see that code is good. The LED changed colors for 5-15 times but then immediately stopped. I know that this application is completely useless but I just want to let it working :).
EDIT2:
I think I will have to use AlarmManager and PendingIntent to launch my service... Tomorrow I will try to do that.
Okay, I think I figured it out. Removed the timer at all. Instead I use AlarmManager :).
I added to onCreate following code:
alarmmgr = (AlarmManager) getSystemService(ALARM_SERVICE);
in onStartCommand:
public int onStartCommand(Intent intent, int flags, int startId)
{
TurnLedOn(); //Turn LED On
myPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent. FLAG_UPDATE_CURRENT);
alarmmgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, myPendingIntent); //Setting alarm to be off after 5seconds.
return Service.START_NOT_STICKY;
}
in onDestroy:
public void onDestroy()
{
notifymgr.cancel(LED_NOTIFICATION_ID); //Clearing notification
alarmmgr.cancel(myPendingIntent); //Clear alarm
}
I think code is ok but I'm totally beginner in Android programming. I think I solved my problem.
You need to use ARGB format. Try to set 0xFFFF0000 - red and 0xFF00FF00 - green. Hope this helps.
Hm, maybe your old notification didn't cleared? Try to use myNotificationManager.clearAll(); Before myNotificationManager.notify(0, notify);
Oh, also, try to set notify.ledOffMS = 5000;

Categories

Resources