我的應用程序由名爲FirstService.Java的服務組成。錯誤關閉應用程序以啓動Android服務
當設備連接要啓動服務 但是一個錯誤的服務鼐啓動:關閉應用
我這個代碼:
StartService.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class StartService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Exemplo Toast", Toast.LENGTH_SHORT).show();
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, FirstService.class);
context.startService(serviceIntent);
}
}
}
的Manifest.xml有它
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.niceplace.liveslink"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-feature />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.niceplace.liveslink.MainActivity"
android:theme="@android:style/Theme.NoTitleBar"
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=".image"
android:theme="@android:style/Theme.NoTitleBar"
android:label="Imagem" >
</activity>
<service
android:name="com.niceplace.liveslink.FirstService"
android:enabled="true"
/>
<receiver android:name=".StartService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
我已將它添加到我的清單中,更多錯誤仍在繼續! –
當我調用服務 –
'if(intent.getAction()。equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)){ }意圖serviceIntent = new Intent(context,FirstService.class); context.startService(serviceIntent); }' –