Unable to vibrate the phone on alarm
I am trying to create a basic Alarm app that vibrates when a user logs in. I just started Android, so this is just a basic app.
I am using the concept of using current difference millisecs
and millisecond of time entered as delay in the .vibrate notification method. But the vibration doesn't happen at the specified time,
This doesn't even happen with some of the other delays I've tried. I do not know what's the problem. I made a simple vibrating notification program a while ago and everything went fine. Any help is appreciated. But this app just forcibly closes when the button is pressed Button
.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v)
{
alarm();
}
protected void alarm()
{
EditText min = (EditText)findViewById(R.id.et1);
EditText hour = (EditText)findViewById(R.id.et3);
EditText sec = (EditText)findViewById(R.id.et2);
long delay = Integer.valueOf(min.toString())*60 +Integer.valueOf(sec.toString())+Integer.valueOf(hour.toString())*3600;
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent i = new Intent(this, alarm2.class); //New intent to launch the other activity that would be launched when the user clicks on the notification!
i.putExtra("id", 2);
PendingIntent p = PendingIntent.getActivity(this, 0, i, 0);//pendingintent p will be launched when the user clicks on notification which is in turn connected to intent i!
Notification notif = new Notification(R.drawable.notif, "Alarm !", System.currentTimeMillis());
notif.setLatestEventInfo(this, "User ALARM !", "Get up Lazy !", p);
notif.vibrate= new long[]{delay*1000 ,500,250,500};
nm.notify(2, notif);
}
@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;
}
}
Alarm2 is a second class that simply performs the task of stopping notification.
Here is the file Manifest
!
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.alarm.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>
<activity
android:name=".alarm2"
android:label="alarm 2" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
source to share
You can try using the notification builder
private static NotificationCompat.Builder buildNotificationCommon(Context _context, .....) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(_context)
.setWhen(System.currentTimeMillis()).......;
//Vibration
builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
//LED
builder.setLights(Color.RED, 3000, 3000);
//Ton
builder.setSound(Uri.parse("uri://sadfasdfasdf.mp3"));
return builder;
}
Also you can try using vibrator
import android.os.Vibrator;
...
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
if none of this works, your mobile phone may not be vibrating or damaged in some way.
For more detailed examples, you can see
http://developer.android.com/training/notify-user/build-notification.html
http://javatechig.com/android/android-notification-example-using-notificationcompat
source to share