2017-05-15 44 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

相关问题