0

我试图在设备启动时启动应用程序。

我的代码如下

1-首先是包含将数据发送到mysql数据库的后台线程(asynctask)的主类。

2- 服务等级

package com.seven.ex.helper;

import com.seven.ex.AndroidGPSTrackingActivity;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class service extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
    return null;
}
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

@Override
public void onStart(Intent intent, int startid)
{
    Intent intents = new Intent(getBaseContext(),AndroidGPSTrackingActivity.class);
    intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intents);
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
}
}    

3- BootUpReceiver 类

package com.seven.ex.helper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootUpReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
    Intent intent = new Intent(arg0,service.class);
    arg0.startService(intent);
    Log.i("Autostart", "started");
}

}

4- 安卓清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpstracking"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

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

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




   <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.seven.gpstracking.AndroidGPSTrackingActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <service android:name=".service" 
              android:label="@string/app_name"
              >
    </service>

    <receiver android:enabled="true" android:name=".BootUpReceiver">

    <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
   </receiver>        

    </application>


   </manifest>

在我尝试在启动时启动它之前,主要方法工作正常。此外,在我忽略启动时的 android 错误后,该应用程序仍在工作(应用程序已关闭)

在日志中 cat 我得到一个类未找到异常

你能帮忙吗?

4

2 回答 2

1

Your Service won't work like it is at the moment. You will have to move the stuff from onStart() to onStartCommand() and then return whether the service is sticky or not. The problem is that the onStart()-method has a good chance of not getting called at all (as it is deprectaed by now).

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {

        //do your stuff here

    return Service.START_NOT_STICKY;
}
于 2013-09-24T14:05:31.243 回答
0

在您的清单中,您已分配包:com.example.gpstracking。当你然后定义.BootUpReceiver。系统应该会在 com.example.gpstracking.BootUpReceiver 找到类。

请尝试将包 .BootUpReceiver 更改为完整路径 com.seven.ex.helper.BootUpReceiver。AndroidGPSTrackingActivity 也是如此,据我所知,这应该是 com.seven.ex.AndroidGPSTrackingActivity。

于 2013-09-24T13:52:10.357 回答