可Android应用程序只播出recevier和服务,而不活动 [英] Can android application have only broadcast recevier and service without activity

查看:135
本文介绍了可Android应用程序只播出recevier和服务,而不活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否Android应用程序只播出recevier和服务没有活动? 如果这是可能的我怎么能调用广播接收器? Android系统自动调用broadcsat接收器?

Can android application have only broadcast recevier and service without activity ? If this is possible how can i invoke broadcast receiver ? Android system automatically invokes the broadcsat receiver ?

$ C $的BroadcastReceiver了C

Code of Broadcastreceiver

  public class CheckReceiver extends BroadcastReceiver {

        public Context con;

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            Toast.makeText(context, "Received", Toast.LENGTH_LONG).show();
            // add PhoneStateListener
            PhoneCallListener phoneListener = new PhoneCallListener();
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            telephonyManager.listen(phoneListener,
                    PhoneStateListener.LISTEN_CALL_STATE);

            con = context;
                                }

        class PhoneCallListener extends PhoneStateListener {

            private boolean isPhoneCalling = false;

            String LOG_TAG = "LOGGING 123";

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {

                if (TelephonyManager.CALL_STATE_RINGING == state) {
                    // phone ringing
                    Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
                }

                if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                    // active
                    Log.i(LOG_TAG, "OFFHOOK");

                    isPhoneCalling = true;
                }

                if (TelephonyManager.CALL_STATE_IDLE == state) {
                    // run when class initial and phone call ended, need detect flag
                    // from CALL_STATE_OFFHOOK
                    Log.i(LOG_TAG, "IDLE");
                    if (isPhoneCalling) {
                        Log.i(LOG_TAG, "restart app");
                        Intent start = new Intent(con, CheckService.class);
                        con.startService(start);
                        isPhoneCalling = false;
                    }

                }
            }
        }
    }

服务code是

Code of Service is

public class CheckService extends Service{

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Intent dialogIntent = new Intent(CheckService.this,SmartDialog.class);
        dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(dialogIntent);


        //Toast.makeText(CheckService.this, "Serive", Toast.LENGTH_LONG).show();
    }

}

Android的清单文件

Android Manifest file is

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

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

       <receiver android:name="CheckReceiver"></receiver>
        <service android:name="CheckService"></service>
        <activity android:name="SmartDialog" android:theme="@android:style/Theme.Dialog"></activity>
    </application>

当我尝试运行此code接收机中,无法启动。任何帮助将是非常有益的。

when i try to run this code receiver is not getting started . any help would be really helpful

推荐答案

与蜂窝开始,BroadcastReceivers安装在停止状态,并且不会触发,直至应用程序已实际运行,即你需要一个活动已至少运行一旦。这是深入解释这个Commonsware博客文章:

Starting with Honeycomb, BroadcastReceivers are installed in a stopped state and will not trigger until an application has actually run, i.e. you will need an activity to have run at least once. This is explained in depth in this Commonsware blog post:

广播回归确认

的Andr​​oid 3.1

这篇关于可Android应用程序只播出recevier和服务,而不活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆