问题描述
我想没有回采运行广播接收器,我不知道怎么办。
I want to run broadcastreceiver without stoping and i dont know how.
我不知道是否有服务运行所有的时间...
I do not know if there is service runs all the time...
Thnaks!
推荐答案
您不需要启动/停止一个BroadcastReceiver。它不是你的后台运行的服务。您只需要注册或注销其用于您的应用程序。注册后,其常开。
You don't need to start/stop a BroadcastReceiver. Its not your background running service. All you need is to register or unregister it for your application. Once registered, its always ON.
在某些特定的事件发生时,系统会通知(广播)有关该事件的所有注册应用程序。所有注册的应用程序接收到这是一个意图
。此外,您可以把您自己的广播。
When some specific event occur, system notifies(broadcast) all registered applications about the event. all registered apps receives this as an Intent
. Also, you can send your own broadcast.
更多,
简单的例子:
在我的表现,我包括许可和一个接收器
in my manifest, I'm including a permission and a receiver
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.receivercall"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".Main">
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
<activity android:name=".TelServices">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
现在,我的接收器, Main.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class Main extends BroadcastReceiver {
String number,state;
@Override
public void onReceive(Context context, Intent intent) {
state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
number=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from : "+number, Toast.LENGTH_LONG).show();
}
else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE))
Toast.makeText(context, "Call ended", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
}
}
在这里,当我安装这个程序,我通过清单注册一个BroadcastReceiver。而面包都会出现有来电/结束时间。
Here, when I install this app, I'm registering a Broadcastreceiver through manifest. And the Toast will appear every time a call comes/ends.
这篇关于我怎么可以运行的BroadcastReceiver不回采?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!