小编cap*_*alf的帖子

从Android中的Activity启动服务时出现"无法启动服务Intent"错误

当我尝试在MyActivity上使用CheckBox"活动来启动名为​​"MyService"的服务时,我在DDMS中看到以下错误:

W/ActivityManager(   73): Unable to start service Intent { cmp=com.example.android.myprogram/.MyService }: not found
Run Code Online (Sandbox Code Playgroud)

我使用了教程http://developer.android.com/resources/tutorials/views/hello-formstuff.html,并将提供的代码添加到我的onCreate()方法的末尾.我在MyActivity.java和MyService.java中单独指定了类.

package com.example.android.myprogram;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;


public class MyActivity extends Activity {
    private static final String TAG = "MyActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
        checkbox.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on …
Run Code Online (Sandbox Code Playgroud)

service android android-intent android-activity

8
推荐指数
1
解决办法
2万
查看次数

是不是需要android.permission.RECEIVE_BOOT_COMPLETED?

有谁知道为什么我的应用程序仍然收到ACTION_BOOT_COMPLETED广播,即使我的应用程序没有android.permission.RECEIVE_BOOT_COMPLETED清单文件中的权限?我认为这是必需的,但我使用的一些教程也没有.一些人做了.我用我的手机运行CyanogenMod进行测试,但我怀疑这很重要.LogCat在每次启动时显示我的"Notified of boot"日志.请参阅下面的代码.

AndroidManifest.xml中

  <receiver android:name="AlarmReceiver">
   <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <category android:name="android.intent.category.HOME" />
   </intent-filter>
  </receiver>
Run Code Online (Sandbox Code Playgroud)

AlarmReceiver类

  public class AlarmReceiver extends BroadcastReceiver {
  private static final String TAG = "MyProgram";

  @Override
  public void onReceive(Context context, Intent intent) {
   try {
          if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
     Log.d(TAG, "Notified of boot");
           }
          Intent newIntent = new Intent(context, MyService.class);
          context.startService(newIntent);
    } catch (Exception e) {
     Log.d(TAG, "An alarm was received but there was an error");
     e.printStackTrace();
     }
    }
  }
Run Code Online (Sandbox Code Playgroud)

我在模拟器上重新审视了这一点,并成功地重现了Android 2.1,2.2和2.3上的"问题".我得到一个ANR(如预期的那样),因为模拟器没有数据库我的app查询.当我从清单中删除所有声明的使用权限时,我在尝试使用我的应用程序时得到了预期的权限拒绝错误.但是,我仍然收到启动时广播的ACTION_BOOT_COMPLETED意图.有什么建议?

boot android broadcastreceiver

4
推荐指数
1
解决办法
5673
查看次数