2016-06-24 5 views
0

私はこの機能をbroadcastreciverで実行したいというアクティビティの機能を持っています。誰でもそれについて知っているし、どのように私はこれを作ったのか教えてください。前もって感謝します。アンドロイド放送受信機で機能を実行するには?

public class Myclass extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 

    } 
} 

これは私がこれを行うにはどのようにいくつかのコードを教えてください私のactivtyにある関数を実行したい私のbroadcastreceiverクラスです。事前に感謝します

答えて

0

私は知っているが、あなたの活動で静的メソッドを呼び出すことができます。あなたの活動で

あなたは次のようにメソッドを宣言します。

YourActivityClass.yourMethod(<input_objs>); 

私はそれが助けを願って:あなたはちょうど呼び出し、この機能を使用することができます受信機では

public static <return_type> yourMethod(<input_objs>){ 
.... 
Your code 
.... 
} 

+0

あなたはいくつかのコードを教えてくれるでしょう –

0

あなたはほぼそこにいます。 Activityでメソッドを作成し、そのメソッドのActivityのインスタンス・コールを使用します。 Activity内のメソッドはプライベートでないことを覚えておいてください。

public class Myclass extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     new YourActivity().yourFunction(); 
    } 
} 

あなたがBroadcastをトリガするには、あなたのActivity、その後

public class Myclass extends BroadcastReceiver{ 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      YourActivity.yourFunction(); 
     } 
    } 

内の静的メソッドを作成したい場合は、Intentを渡す必要があります。あなたはどのActivity

Intent intent = new Intent(); 
this.sendBroadcast(intent); 

から、それをトリガーにしたい場合は、Fragmentその後、

Intent intent = new Intent(); 
getActivity().sendBroadcast(intent); 
+0

どのように静的メソッドを作成するのですか? –

+0

メソッドの戻り型の前にキーワード_static_を使用する – Prudhvi

0

からBroadcastをトリガしたい場合は、Myclassでインターフェースを宣言し、あなたのMainActivity

でそれを実装することができます
public class Myclass extends BroadcastReceiver{ 

    public interface MyClassInterface { 
     void onMyClassReceive(); 
    } 

    private MyClassInterface mListener; 

    public Myclass(MyClassInterface mMyClassInterface) { 
     mListener = mMyClassInterface; 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     mListener.onMyClassReceive(); 
    } 
} 

次にあなたのMainActivity

public class MainActivity implements Myclass.MyClassInterface { 

private mMyClass Myclass = new Myclass(this); 
    @Override 
    public void onMyClassReceive() { 
     // Do stuff when Myclass.onMyClassReceive() is called, 
     // which will be called when Myclass.onReceive() is called. 
    } 
} 
1

実行するメソッドにアクティビティインスタンスが必要な場合は、アクティビティの状態と機能にアクセスできるように、アクティビティ内にブロードキャスト受信者を登録できます。

final IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction("Your Intent action here"); 
    intentFilter.addAction("Another action you want to receive"); 

    final BroadcastReceiver myReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      theFunctionYouWantToExecute(); 
     } 
    }; 

    registerReceiver(myReceiver, intentFilter); 

そして、あなたの「onDestroy」方法で:あなたのアクティビティ「のonCreate」メソッドで

unregisterReceiver(myReceiver); 

は、この場合には、あなたの放送受信機は、あなたの活動状態への完全なアクセス権を持っていることに注意してください、しかしライフサイクルはアクティビティライフサイクルに調整されます。 別のオプションとして、アクティビティメソッドを静的として宣言することができます。そのため、アプリケーションのどの部分でも実行できます。

関連する問題