我正在尝试构建一个基本的警报应用程序,在用户输入时振动。我刚开始使用Android,所以它只是一个基本的应用程序。
我使用的概念是使用当前millisecs
的差异和输入时间的毫秒作为notification.vibrate方法的延迟。但是,振动不会发生在进入时间。
它甚至没有发生在我试过的其他延迟上。我不知道问题是什么。我做了一个简单的通知振动程序一段时间后,它运行得很好。任何帮助表示赞赏。但是这个应用程序只是在单击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是第二个完成终止通知任务的类。
这是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>
答案 0 :(得分:0)
您可以尝试使用通知构建器
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;
}
您也可以尝试使用vibrator
import android.os.Vibrator;
...
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
如果这些都不起作用,那么您的手机可能没有启用振动或者某种程度上已经损坏
有关详细示例,请参阅
http://developer.android.com/training/notify-user/build-notification.html
http://javatechig.com/android/android-notification-example-using-notificationcompat