0

我编写了一个与 Windows Azure 移动服务通信的 Android 应用程序,通常在设备和 android Azure 数据库之间发送数据,但是当我插入相同的代码时,启动时自动启动的服务不会运行。我对启动时启动的 Android 服务没有任何问题。我对 Windows Azure Services Android 服务没有任何问题。

但是当两者之一(服务在启动时启动 Android + Windows Azure 服务)不运行。

我需要帮助!

ItemReceiver.java

package com.example.itemreceiver;

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

public class ItemReceiver extends BroadcastReceiver {   

    @Override
    public void onReceive(Context context, Intent intent) {
      Intent myIntent = new Intent(context, ItemService.class);
      context.startService(myIntent);
    }
}

项目服务.Java

package com.example.itemreceiver;

import java.net.MalformedURLException;
import com.microsoft.windowsazure.mobileservices.*;
import android.util.Log;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class ItemService extends Service  {
    private static final String TAG = "++Service++";
    private MobileServiceClient mClient;

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

    @Override
    public void onStart(Intent intent, int startId) {

        // TODO Auto-generated method stub
         try {
             mClient = new MobileServiceClient( 
                     "https://bn7.azure-mobile.net/",
                      "nFdklchhdEhCuUsmSZVsxgYLPeaOLo64",
                    this 
                    );
            Item item = new Item(); 

            // Phone Number
            item.IdPhone = "005511964271485";

            // IMEI
            item.IdImei =  "351597055788723";

            item.DateTimePhone = "2013-05-03 14:00:00"; 

            item.Active = false; 
            mClient.getTable(Item.class).insert(item, new TableOperationCallback<Item>() {
            @Override 
            public void onCompleted(Item entity, Exception exception, ServiceFilterResponse response)               { 
            if (exception == null) { 
                         // Insert succeeded
                        Log.d(TAG, " Insert Sucess");
                                            } else { 
                         // Insert failed
                        Log.d(TAG, " Insert failed");
                        Log.d(TAG, " toString = " + exception.toString());
                        Log.d(TAG, " getCause = " + exception.getCause());
                        Log.d(TAG, " getStackTrace = " + exception.getStackTrace());
                    } 
                 } 
                });
       } catch (MalformedURLException e) {
           Log.d(TAG, " MalformedURLException " + e.toString());
       }
       this.stopSelf();
    }

      @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            Log.d(TAG, "FirstService destroyed");
        }
}

显现

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bn7.rotareceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name=".RotaReceiver" android:enabled="true" android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

        <service android:name=".ServiceRota"  android:process=":Service_Rota"></service>
    </application>

</manifest>

错误 Exception.toString = com.microsoft.windowsazure.mobileservices.MobileServiceException:处理请求时出错。Exception.getCause = java.net.UnknownHostException: bn7.azure-mobile.net Exception.getStackTrace = [Ljava.lang.StackTraceElement;@40519770

4

1 回答 1

1

正如错误描述已经表明的那样,您在解析移动服务 URL 时遇到问题,而不是移动服务本身。

我的建议是您的 Android 设备在启动完成事件时没有建立 Internet 连接。在实例化之前尝试检查 Internet 连接MobileServiceClient是相应的代码。如果这是问题所在,请尝试推迟实例化或等到服务中的连接。

于 2013-05-06T10:13:14.873 回答