Android:Custom View是否具有startService

时间:2015-10-27 23:27:22

标签: android android-intent android-intentservice

我有一个自定义视图,单击时将图像上传到服务器。

我使用getContext获取上下文并使用它来调用启动intent服务。不知何故,当我在onHandleIntent或onStartCommand上创建检查点时,服务没有开始。我在清单中放置了一个引用。

这是对服务的代码调用,其中代码位于扩展ImageView的自定义视图中:

new GraphicUtilitiesV2.SimpleImageLoadingCallback() {
                                    @Override
                                    public void imageAvailable(Object sender, String data) {

                                        if (_formCOC != null && _formType != null) {
                                            Gson gson = new Gson();
                                            String formCOC = gson.toJson(_formCOC);
                                            UploadAttachmentService.startActionAttachmentDeclarative
                                                    (getContext(), formCOC, _formType.getValue(), data);
                                        }
                                    }
                                });

这是我的AttachmentUploadService类的演示代码:

private static final String ACTION_DECLARATIVE = "blah.Service.action.DeclarativeAttachment";

private static final String EXTRA_FORM = "blah.CERTIFI.Service.extra.Form";
private static final String EXTRA_TYPE = "blah.Service.extra.FormType";
private static final String EXTRA_DATA = "blah.Service.extra.Data";

public static void startActionAttachmentDeclarative(Context context, String form, String type, String data) {
    Intent intent = new Intent(context, UploadAttachmentService.class);
    intent.setAction(ACTION_DECLARATIVE);
    intent.putExtra(EXTRA_FORM, form);
    intent.putExtra(EXTRA_TYPE, type);
    intent.putExtra(EXTRA_DATA, data);
    context.startService(intent);
}

public UploadAttachmentService() {
    super("UploadService");
}

@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        final String action = intent.getAction();
        if (ACTION_DECLARATIVE.equals(action)) {
            final String param1 = intent.getStringExtra(EXTRA_FORM);
            final String param2 = intent.getStringExtra(EXTRA_DATA);
            handleActionAttachmentDeclarative(param1, param2);
        }
    }
}

public void handleActionAttachmentDeclarative(String form, String data) {
    try {
        Thread.sleep(50000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

也没有错误或强制关闭。

这是我明显的部分:

<service
        android:name=".Service.UploadAttachmentService"
        android:exported="false" >
    </service>

UploadAttachmentService文件存储在Service文件夹中。

0 个答案:

没有答案