程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> service-[Android]Service裡面發送廣播消息出現問題[內有代碼]

service-[Android]Service裡面發送廣播消息出現問題[內有代碼]

編輯:編程綜合問答
[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移動問答

最佳回答:


  <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>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved