这是你需要的。创建一个BroadCastReceiver
像这样的片段:
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent it = new Intent();
it.setAction("your.package.name.MyBroadcastReceiver");
context.startService(it); //Start a service
}
}
并将其添加到您的清单中:
<receiver android:name="your.package.name.MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
<category android:name="android.intent.category.HOME" >
</category>
</intent-filter>
</receiver>
然后像这样创建一个服务
public class YourService extends Service {
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = { 0, 300, 0 };
v.vibrate(pattern, 0); // Use this to vibrate
// rdlVibrator.cancel(); Use this to stop the vibration
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
最后将其添加到您的清单文件中:
<service
android:name="your.package.name.YourService"
android:process=":my_process" >
<intent-filter>
<action android:name="your.package.name.YourService" >
</action>
</intent-filter>
</service>
如果您有任何问题,请提出。希望能帮助到你。