拒绝通话后是否可以开始活动?

时间:2013-08-13 17:10:39

标签: android android-activity

我的问题有点简单,但作为一个初学者,当电话呼叫被拒绝时(按下红色按钮时)我无法找到开始我的活动的方法。 我在过去的30分钟里看startActivityForResult(),但对我来说似乎不可能。

你有想法吗?我很确定它可以轻松完成,但我无法找到正确的方法。

2 个答案:

答案 0 :(得分:3)

我们很有可能做得比这更好。

我在Eclipse上玩了一下,这就是我设法做的事情。

MainActivity没用。如果有的话,可用于插入按钮来激活服务。

AfterActivity是在通话结束时启动的。

该应用程序的核心是phoneBroadcast。在此代码中,您可以看到呼叫的管理和应用程序的启动。

请注意清单中的uses-permission和android操作:

android.intent.action.BOOT_COMPLETED

用于phoneBroadcast接收器。这样即使重启后也能自动启动接收器phoneBroadcast。

MainActivity.java:

/*
 * AfterCall - simple demo for StackOverflow
 * 2013 by Felice Murolo
 */

package com.fmtec.android.aftercall;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

AfterActivity.java:

package com.fmtec.android.aftercall;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class AfterActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.after, menu);
        return true;
    }

}

phoneBroadcast.java:

package com.fmtec.android.aftercall;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;


public class phoneBroadcast extends BroadcastReceiver {
    private static final String TAG = "AfterCall-broadcastReceiver";

    public phoneBroadcast() {
        Log.d(TAG,"I'm into broadcast");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG,"broadcastManager Receive");
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        phoneStateListener customPhoneListener = new phoneStateListener();
        telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        Log.d(TAG,"CallState: "+telephony.getCallState());

        /* YOUR ACTIVITY WAS LAUNCHED HERE */
        if (telephony.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
            Intent i = new Intent(context,AfterActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

phoneStateListener.java:

package com.fmtec.android.aftercall;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class phoneStateListener extends PhoneStateListener {

    private static final String TAG = "AfterCall-phoneStateListener";

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

        //if (incomingNumber.length()>0) Log.d(TAG, incomingNumber);

        switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d(TAG, "RINGING");
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            Log.d(TAG, "IDLE");
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            Log.d(TAG, "OFFHOOK");
            break;
        }   
    }
}

的strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">AfterCall</string>
    <string name="action_settings">Settings</string>
    <string name="message">Hello, I\'m the MainActivity.</string>
    <string name="message_after">Hello, I\'m the AfterActivity. I will show to you after a phonecall ending.</string>
    <string name="title_activity_after">AfterActivity</string>
    <string name="hello_world">Hello world!</string>
</resources>

的AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="10"
        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" >
        <activity
            android:name="com.fmtec.android.aftercall.MainActivity"
            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="com.fmtec.android.aftercall.phoneBroadcast" >
            <intent-filter android:priority="999" >
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <activity
            android:name="com.fmtec.android.aftercall.AfterActivity"
            android:label="@string/title_activity_after" >
        </activity>
    </application>

</manifest>

activity_main.xml(布局MainActivity)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:text="@string/message"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

activity_after.xml(布局AfterActivity)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".AfterActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="@string/message_after"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

答案 1 :(得分:1)

具有对呼叫状态的访问权限的正在运行的服务将能够做这样的事情。