Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在不停止的情况下运行broadcastreceiver?_Android_Broadcastreceiver - Fatal编程技术网

Android 如何在不停止的情况下运行broadcastreceiver?

Android 如何在不停止的情况下运行broadcastreceiver?,android,broadcastreceiver,Android,Broadcastreceiver,我想不停地运行广播接收器,我不知道如何运行 我不知道是否有服务一直在运行 塞纳克斯 您不需要启动/停止广播接收器。这不是你的后台运行服务。您只需为应用程序注册或注销它。一旦注册,它就会一直打开 当某个特定事件发生时,系统通知(广播)所有注册的应用程序该事件。所有已注册的应用程序都将此作为意图接收。此外,您还可以发送自己的广播 更多 简单的例子: 在我的清单中,我包括一个许可和一个接收者 <manifest xmlns:android="http://schemas.android.com/

我想不停地运行广播接收器,我不知道如何运行

我不知道是否有服务一直在运行


塞纳克斯

您不需要启动/停止广播接收器。这不是你的后台运行服务。您只需为应用程序注册或注销它。一旦注册,它就会一直打开

当某个特定事件发生时,系统通知(广播)所有注册的应用程序该事件。所有已注册的应用程序都将此作为
意图
接收。此外,您还可以发送自己的广播

更多

简单的例子:

在我的清单中,我包括一个许可和一个接收者

<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>
在这里,当我安装这个应用程序时,我正在通过清单注册一个Broadcastreceiver。每次通话结束时都会出现祝酒词

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();

    }

}