2 k lord K_Lord 于 2013.06.07 14:41 提问

[Android]Service里面发送广播消息出现问题[内有代码]
    package com.sample;

// MainActivity.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private Button btnStart;
    private Button btnStop;
    private EditText editShow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnStart = (Button)findViewById(R.id.startButton);
        btnStop = (Button)findViewById(R.id.stopButton);
        editShow = (EditText) findViewById(R.id.editShow);

        btnStart.setOnClickListener(startListener);
        btnStop.setOnClickListener(stopListener);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(".myBroadcastAction");
        this.registerReceiver(new myBroadcastReciver(), intentFilter);
    }
    private OnClickListener startListener = new OnClickListener() {

        public void onClick(View v) {
            startService(new Intent(MainActivity.this, BackgroundService.class));
            }
    };
    private OnClickListener stopListener = new OnClickListener() {

        public void onClick(View v) {
            stopService(new Intent(MainActivity.this,BackgroundService.class));
        }
    };

    private class myBroadcastReciver extends BroadcastReceiver{

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            String str = "null";
            if(arg1.getAction().equals(".myBroadcastAction")){
                str = arg1.getStringExtra("data");
            }
            editShow.setText(str);
        }

    }
}

//service

package com.sample;

// BackgroundService.java

import java.io.IOException;

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

public class BackgroundService extends Service {
    private String sMsg = "";
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Crete", Toast.LENGTH_SHORT).show();       
        System.out.println("Create");       
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        super.onStartCommand(intent, flags, startId);
        Toast.makeText(this, "service Start", Toast.LENGTH_SHORT).show();
        System.out.println("service start");
        startThread();//启动线程
        return START_STICKY;

    }

    private void startThread() {
        new Thread(){
            public void run(){
                Intent intent = new Intent();
                int i = 0;
                while(true){
                    try{
                        Thread.sleep(5000);
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    i++;
                    intent.setAction(".myBroadcastAction");
                    intent.putExtra("data", i+"");
                    sendBroadcast(intent);
                }
            }
        }.start();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "service stop", Toast.LENGTH_SHORT).show();
        System.out.println("service stop");
    }
}

//xml

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 广播注册 -->
        <receiver android:name = ".broadcastReciver">
            <intent-filter android:priority="20">
                <action android:name=".myBroadcastAction"/>
            </intent-filter>
        </receiver>
        <service android:name="com.sample.BackgroundService" /> 

    </application>   
</manifest>

错误信息:
CSDN移动问答

1个回答

A328240784
A328240784   2013.06.07 14:50
已采纳
  <receiver android:name = ".broadcastReciver">
            <intent-filter android:priority="20">
                <action android:name=".myBroadcastAction"/>
            </intent-filter>
        </receiver>


 <receiver android:name = ".myBroadcastReciver">
            <intent-filter android:priority="20">
                <action android:name=".myBroadcastAction"/>
            </intent-filter>
        </receiver>
K_Lord
K_Lord myBroadcastReciver是可以不用配置的
4 年多之前 回复
K_Lord
K_Lord 按这样改过了,还是原来那个问题!
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!