[英]how to send data from broadcast receiver to activity in android
Here is what I want to do. 这就是我想要做的。 I have created a broadcastreceiver class which detects incoming calls through callrecieve activity. 我创建了一个broadcastreceiver类,该类通过callrecieve活动检测传入的呼叫。 Now as soon as a call comes I want to find its current time and display it through another activity. 现在,一旦有电话打来,我想找到它的当前时间并通过其他活动显示它。 I have used intents for this. 我已经为此使用了意图。 But still I am not getting the time value as required, displayed when I receive a call. 但是我仍然没有得到我收到呼叫时显示的所需时间值。 I have posted all the codes I am using. 我已经发布了我正在使用的所有代码。 any help would be great 任何帮助都会很棒
Here is the broadcast receiver activity which checks the state of phone for incoming calls and is responsible for broadcasting the intent 这是广播接收器活动,它检查电话状态以查看传入呼叫并负责广播意图
package com.mohit;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class callreceive extends Activity {
public class receive extends BroadcastReceiver {
long start_time,end_time,dif_sec;
int flag=0;
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
if ((state.equals(TelephonyManager.EXTRA_STATE_IDLE))&&flag==0)
{
flag=1;
start_time=System.currentTimeMillis();
Intent intent1 = new Intent();//(callreceive.this,TestActivity.class);
intent1.setAction(Intent.ACTION_SEND);
intent1.putExtra("start_time",start_time);
startActivity(intent1);
}
}
}
}
}
Now i want to send the value of start_time to an activity as soon as if loop is executed. 现在,我想一旦执行循环就将start_time的值发送给活动。 Here is the code for the receiving activity-Test Activity. 这是接收活动-测试活动的代码。
package com.mohit;
import com.mohit.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class TestActivity extends Activity {
TextView output1,output2;
long start_time,end_time;
int flag=0;
long dif_sec=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output1=(TextView)findViewById(R.id.textView1);
Intent extras = getIntent();
if (extras!=null)
{
if (flag==0)
{
flag=1;
dif_sec=extras.getLongExtra("start_time",0);
}
}
output1.setText(String.valueOf(dif_sec));
}
}
Here is the android manifest file 这是Android清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mohit"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity android:name="TestActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="callreceive" >
<receiver android:name="receive" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.PHONE_STATE"></action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</activity>
</application>
</manifest>
I am unable to print the value of start_time through test acitvity. 我无法通过测试活动性来打印start_time的值。 Am I missing something or is something wrong?? 我错过了什么还是出了什么问题? Plz Help THanx 请帮助THanx
create new Activity called NewActivity. 创建名为NewActivity的新Activity。 and do this: 这样做:
public class callreceiver extends BroadcastReceiver {
long start_time;
int flag=0;
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
if ((state.equals(TelephonyManager.EXTRA_STATE_RINGING))&&flag==0)
{
flag=1;
start_time=System.currentTimeMillis();
Intent intent = new Intent(context,NewActivity.class);
intent.putExtra("start_time",start_time)
startActivity(intent);
}
}
Now, create a new class that get the extras: 现在,创建一个新的类以获取更多信息:
public class ILoveUrielFrankelActivity extends Activity {
private TextView mTextView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
long displayTime = extras.getLong("start_time");
TextView mTextView = (TextView)findViewById(R.id.uriel);
mTextView.setText(displayTime);
}
and inside res/layout/main.xml 并在res / layout / main.xml内部
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/uriel"/>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.