gpt4 book ai didi

android - 拒接电话后是否可以开始 Activity ?

转载 作者:行者123 更新时间:2023-11-29 16:06:06 25 4
gpt4 key购买 nike

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

你有想法吗?我很确定这很容易完成,但我无法找到正确的方法。

最佳答案

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

我尝试了一下 Eclipse,这就是我设法做到的。

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

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

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

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

android.intent.action.BOOT_COMPLETED

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

主要 Activity .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;
}

}

电话广播.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);
}
}
}

电话状态监听器.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;
}
}
}

字符串.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>

关于android - 拒接电话后是否可以开始 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18215226/

25 4 0
文章推荐: android - ISO15693/Tag-it HF-I 收发失败
文章推荐: mysql - 如何对具有多行的列的数量求和
文章推荐: php - 具有分层邻接模型 MySql 和 PHP 的缩进 HTML