2017-05-15 47 views
-3

當用戶通過使用boradcast接收器以編程方式關閉手機時,我無法檢測到手機的狀態。檢測當Android設備在關閉後打開位置

在此先感謝!

+0

請張貼一些代碼,確保您註冊的是BroadcastReceiver。 – Phillen

+0

其實我不知道如何檢測設備開機狀態 – Puri

+0

你的意思是說..你想在Android設備打開時運行廣播嗎? – Naitik

回答

0

以下代碼適用於我!希望會對你有所幫助。

的AndroidManifest.xml

<receiver android:name=".BootCompletedReceiver" > 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
    </intent-filter> 
</receiver> 

<service android:name="NotifyingDailyService" > 
</service> 

BootCompletedReceiver.class

public class BootCompletedReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent arg1) { 
    // TODO Auto-generated method stub 
    Log.w("boot_broadcast_poc", "starting service..."); 
    context.startService(new Intent(context, NotifyingDailyService.class)); 
} 

} 

硒rvice.class

public class NotifyingDailyService extends Service { 

@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public int onStartCommand(Intent pIntent, int flags, int startId) { 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show(); 
    Log.i("com.example.bootbroadcastpoc","NotifyingDailyService"); 

    return super.onStartCommand(pIntent, flags, startId); 
} 
} 
+0

其工作正常謝謝! – Puri

相關問題