秀小川 2013-06-07 06:41 采纳率: 88.9%
浏览 7118
已采纳

[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条回答

  • 追风筝的孩子 2013-06-07 06: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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?