java - 将 Windows Azure Mobile 与启动时启动的 Android 服务一起使用是否存在限制?

标签 java android azure azure-mobile-services

我编写了一个与 Windows Azure 移动服务通信的 Android 应用程序,通常在设备和 Android Azure 数据库之间发送数据,但是当我插入相同的代码时,启动时自动启动的服务不会运行。 我对开机启动的 Android 服务没有任何问题。 我对 Windows Azure 服务 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);
    }
}

ItemService.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");
        }
}

list

<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

最佳答案

正如错误描述所示,您遇到的问题是解析移动服务 URL,而不是移动服务本身。

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

关于java - 将 Windows Azure Mobile 与启动时启动的 Android 服务一起使用是否存在限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16366231/

相关文章:

java - 如何在Wowza服务器的publish方法中停止流媒体

java - 如何避免Google App Engine不支持Runtime.addShutdownHook

android - 如何在 Android 的 Canvas 上绘制平滑/抖动渐变

java - 将变量值存储在数组中以查找平均速度

java - 从输入更改为扫描仪

java - 不可转换的类型无法转换

android - RecyclerView 滚动到没有动画的所需位置

Azure 表存储 - 用于快速查询的分区键设计

azure - 通过postman调用microsoft graph api

Azure 传统负载均衡器 VS Azure 应用程序网关响应延迟?