這是您需要的。這樣的片段創建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>
如果您有任何問題,請詢問。希望能幫助到你。
感謝您的反饋,但我正在尋找更早的啓動順序。我們的應用程序在啓動時運行並自我介紹,但我們需要讓盲人知道他們已按下on按鈕足夠長的時間才能啓動引導。 – user2901405