2012-02-21 39 views
5

Tôi đang cố gắng phát một tin nhắn chúc mừng với đoạn mã sau mở rộng Activity. Nhưng phát sóng không được nhận bởi một Activity, bánh mì nướng không được hiển thị. Ai đó có thể giải quyết được lỗi của tôi không? Các hoạt động chính là SendBroadcast.javaBroadcastReceiver Không nhận được Broadcast

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 

public class SendBroadcast extends Activity { 

    public static String BROADCAST_ACTION = 
          "com.unitedcoders.android.broadcasttest.SHOWTOAST"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void sendBroadcast(View v) { 
     Intent broadcast = new Intent(); 
     broadcast.setAction(BROADCAST_ACTION); 
     sendBroadcast(broadcast); 
    } 
} 

Hoạt động Toast hiển thị là ToastDisplay.java

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.widget.Toast; 

public class ToastDisplay extends Activity { 

    private BroadcastReceiver receiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      Toast.makeText(getApplicationContext(), "received", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }; 

    @Override 
    protected void onResume() { 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(SendBroadcast.BROADCAST_ACTION); 
     registerReceiver(receiver, filter); 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     unregisterReceiver(receiver); 
     super.onPause(); 
    } 
} 

manifest.java như sau

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.broad" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <uses-sdk android:minSdkVersion="3" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".SendBroadcast" 
      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=".ToastReceiver" > 
      <intent-filter> 
       <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST" /> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 

Trả lời

9

Có thể có hai loại broacast: tĩnh và động. Tĩnh là những thứ được khai báo trong tệp kê khai. Năng động có thể được khai báo trong thời gian chạy. Bạn đã kết hợp hai loại phát sóng này trong một chương trình phát sóng.

Để khai báo một chương trình phát động đơn giản, bạn có thể sử dụng mã sau (dựa trên mã của bạn). Nó sẽ chỉ hiển thị thông điệp bánh mì nướng khi hoạt động được hiển thị.

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.util.Log; 
import android.view.View; 
import android.widget.Toast; 

public class BroadcastTestActivity extends Activity { 

    public static String BROADCAST_ACTION =  
          "com.unitedcoders.android.broadcasttest.SHOWTOAST"; 
    BroadcastReceiver br = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.w("Check", "Inside On Receiver"); 
      Toast.makeText(getApplicationContext(), "received", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(BROADCAST_ACTION); 
     filter.addCategory(Intent.CATEGORY_DEFAULT); 
     registerReceiver(br, filter); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     sendBroadcast(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     unregisterReceiver(br); 
    } 

    public void sendBroadcast() { 
     Intent broadcast = new Intent(); 
     broadcast.setAction(BROADCAST_ACTION); 
     broadcast.addCategory(Intent.CATEGORY_DEFAULT); 
     sendBroadcast(broadcast); 
    } 
} 

Vì vậy, thay vì hiển thị bánh mì nướng, bạn có thể gọi cho hoạt động mới của mình. Các hành động sau tùy thuộc vào nhu cầu của bạn (những gì bạn muốn làm).

+0

ok anh trai u có thể cho tôi biết tôi phải loại bỏ mà phát sóng tĩnh từ biểu hiện và u có thể cho tôi một gợi ý những gì sẽ là phần còn lại của tôi .java tập tin –

+0

Nó phụ thuộc vào những gì bạn muốn làm. Tôi đã cho bạn thấy một ví dụ rất đơn giản làm thế nào để hiển thị một tin nhắn chúc mừng được hiển thị sau khi phát sóng đã được nhận. Bạn có thể chỉ cần tạo một dự án mới và kiểm tra lớp này. – Yury

+0

trước hết là tôi muốn hiển thị TOast động –

0

Hãy thử

<activity android:name=".SendBroadcast" android:label="@string/app_name"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.BROADCAST" /> 
    </intent-filter> 
</activity>  

Trong onCreate() gọi

sendBroadcast(v); 
+0

anh chàng không làm việc cố gắng –

+0

không anh –

+0

làm việc gì là '

3

đâu ToastReceiver lớp học?

<receiver android:name=".ToastReceiver"> 
    <intent-filter> 
     <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/> 
</intent-filter> 
</receiver>` 

Thay đổi

public class ToastDisplay extends Activity { 
    private BroadcastReceiver receiver = new BroadcastReceiver() { 

} 

để

public class ToastReceiver extends BroadcastReceiver { 

} 
+0

không làm việc anh trai –

-2
Button b1 = (Button)findViewById(R.id.button1); 

b1.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     try { 
      String RESULT = new TestAsyncTask().execute(" ").get(); 
      System.out.println("RESULT "+RESULT); 
     } 
     catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
}); 
Các vấn đề liên quan