错误关闭应用程序以启动Android服务

时间:2016-07-29 22:17:22

标签: java android

我的应用程序由名为 FirstService.Java 的服务组成。

设备连接时希望服务启动 但服务nai开始出错:关闭应用程序

我的代码:

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>

2 个答案:

答案 0 :(得分:0)

我认为你必须在你的AndroidManifest.xml中添加RECEIVE_BOOT_COMPLETED权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

答案 1 :(得分:0)

设法解决了这个问题,这是我的脚本中寻找GetCookie cookie的一行

当你启动服务时,它不是cookie!但如果我进入申请,他就会获得cookie!

var dbf = fileDlg.FileName;
var options = new DbContextOptionsBuilder();
options.UseSqlServer($@"Server=(localdb)\mssqllocaldb;Initial Catalog=master;MultipleActiveResultSets=False;Integrated Security=True");
using (var master = new DbContext(options.Options))
{
     master.Database.ExecuteSqlCommand($"ALTER DATABASE [{dbf}] SET OFFLINE WITH ROLLBACK IMMEDIATE");
     master.Database.ExecuteSqlCommand($"exec sp_detach_db '{dbf}'");
            }

感谢所有试图帮助我的人!