2011-08-29 1 views
0

방송 수신기를 사용하는 동안 몇 가지 문제가 있습니다.Android. 방송 수신기

대상 : 다음 스키마를 사용할 수있는 세 개의 앱이 있습니다. 1. 먼저 메시지를 받으면 데이터베이스에 데이터를 쓸 수있는 브로드 캐스트 리시버 앱입니다. 2. 두 번째 - 안드로이드는 데이터베이스에 저장해야하는 데이터로 일부 의도를 보냅니다. 3. 셋째 - 홈 화면의 위젯으로 데이터베이스에 저장해야하는 데이터로 일부 의도를 보냅니다.

그래서, 나는 일식에 세 가지 응용 프로그램을 만듭니다. 1 BroadcastReceiverExample - 방송 수신기는 다음 파일

package com.test.receive; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.widget.Toast; 

public class SimpleReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "service get started action", Toast.LENGTH_LONG).show(); 
     Log.e("START","START"); 

    } 

} 

및 매니페스트 파일 소스를 가지고 또한 내가 이클립스 에서 앱 프로젝트 (BroadcastSenderExample)를 생성하고 다음에 보낸 코드

와 파일이

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

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <receiver android:enabled="true" android:name=".receive.SimpleReceiver" android:exported="false"> 
      <intent-filter android:priority="999"> 
       <action android:name="com.test.SIMPLE_TEST_SERVICE"></action> 
      </intent-filter> 
     </receiver> 

    </application> 
</manifest> 

package com.test.sender; 

import com.test.R; 

import android.app.Activity; 
import android.content.BroadcastReceiver; 
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; 

public class BroadcastSenderExample extends Activity { 

    public final static String ACTION="com.test.SIMPLE_TEST_SERVICE"; 
    public final static String TYPE="type"; 
    public final static int START=1; 
    public final static int STOP=0; 

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

     btnStart=(Button)findViewById(R.id.btnStart); 
     btnStart.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent bcIntent=new Intent(ACTION); 
       sendBroadcast(bcIntent); 
      } 
     }); 
     btnEnd=(Button)findViewById(R.id.btnEnd); 
     btnEnd.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent bcIntent=new Intent(ACTION); 
       sendBroadcast(bcIntent); 
      } 
     }); 
    } 

    private Button btnStart=null; 
    private Button btnEnd=null; 

} 

그런 다음 기기에 첫 번째 앱을 설치하고 (에뮬레이터도 시도합니다) 두 번째 앱을 설치합니다. 그리고 나서 두 번째 앱 실행 인 텐트 콜이 발생하지 않습니다.

내가 뭘 잘못하고 있니?

나는 다음 코드

프로젝트 하나 wBRReceiver

파일 WBRReceiver.java

패키지 com.x.brreceiver 두 개의 프로젝트를 만들;

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.widget.Toast; 

public class WBRReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
     Log.i("THIS IS A TEST RECEIVER","THIS IS A TEST RECEIVER"); 
     Toast.makeText(arg0, "this is a test receiver", Toast.LENGTH_LONG).show(); 
    } 

} 

의 AndroidManifest.xml

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

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <receiver android:name="WBRReceiver"> 
      <intent-filter> 
       <action android:name="com.x.START"></action> 
      </intent-filter> 
     </receiver> 

    </application> 
</manifest> 

그리고 두 wBRSender

파일 프로젝트 WBRSenderActivity.java

package com.x.brsender; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

public class WBRSenderActivity extends Activity { 

    private String ACTION_NAME="com.x.START"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Intent brr=new Intent(ACTION_NAME); 
     //I can't use this 
     //brr.setClass(this, WBRReceiver.class); 
     //Because i just don't have this class in this case 
     sendBroadcast(brr); 
    } 
} 

그리고 매니페스트

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.x.brsender" 
     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=".WBRSenderActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
</manifest> 

그리고 나서 에뮬레이터에 첫 번째 앱을 설치 한 다음 두 번째 앱을 실행하십시오. 그리고 그것은 작동합니다.

답변

1

logcat 출력을 보셨습니까? 무엇이 잘못되었는지를 알려주는 좋은 기회가 있습니다.

코드를 너무 많이 보지 않고도 매니페스트가 손상된 것으로 보입니다. 수신자에서 android : name은 ".receive.SimpleReceiver"입니다.이 값 (a.로 시작하는 경우)은 단순히 "Android 패키지 이름을 따르는 부분이 아닙니다."- 그런 식으로 작동하지만 대부분의 경우. 귀하의 경우 귀하의 안드로이드 패키지는 "com.test"이지만 수신자가 들어있는 패키지는 "com.test.receive"입니다.SimpleReceiver "이고 Java 패키지는"com.test.receive "입니다. android : 이름을"com.test.receive.SimpleReceiver "로 변경해보십시오.