Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 为什么当我收到广播意图时,我的主要活动会显示出来_Android_Android Activity_Android Broadcast - Fatal编程技术网

Android 为什么当我收到广播意图时,我的主要活动会显示出来

Android 为什么当我收到广播意图时,我的主要活动会显示出来,android,android-activity,android-broadcast,Android,Android Activity,Android Broadcast,我有两个活动,分别是main活动和RecordHistory 我在main活动中启动广播接收器 我也能在接收时获得广播意图 我正在启动RecordHistory活动。 但我的问题是main活动在RecordHistory可见时也会启动。 但我只想启动RecordHistory活动 REcordHistory.java Bundle bundle = getIntent().getExtras(); String phonenumbString = bundle.getString("ph

我有两个活动,分别是
main活动
RecordHistory

我在
main活动中启动广播接收器

我也能在接收时获得广播意图 我正在启动
RecordHistory
活动。
但我的问题是
main活动
RecordHistory
可见时也会启动。
但我只想启动
RecordHistory
活动

REcordHistory.java

Bundle bundle = getIntent().getExtras();
    String phonenumbString = bundle.getString("phonenumber");
    Toast.makeText(this, phonenumbString, Toast.LENGTH_SHORT).show();

    final Dialog dialog = new Dialog(RecordHistory.this);
    Window window = dialog.getWindow();
    WindowManager.LayoutParams wlp = window.getAttributes();

    wlp.gravity = Gravity.CENTER;
    wlp.x=0;
    wlp.y=0;
    //
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.show);
    dialog.setCanceledOnTouchOutside(true);
    dialog.setCancelable(true);
    dialog.show();
}
MainActivity.java

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

    broadcastIntent();
}

public void broadcastIntent() {
    Intent intent = new Intent();
    //Toast.makeText(MainActivity.this,"Running ",Toast.LENGTH_LONG).show();
    intent.setAction("com.example.downloadfileservice.IncomingCallListener");
    sendBroadcast(intent);
}

public void onClick(View view) {

}
}   
IncomingCallReceiver.java

public class IncomingCallListener extends BroadcastReceiver {
private static long timeStarted = -1L; // IMPORTANT!
private static long timeAnswered;
private static long timeEnded;
private static boolean isRoaming;
private static String callerNumber;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.d("TEST","OnRecieve ");
Bundle extras = intent.getExtras();
if (extras == null) return;
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state == null) return;

// phone is ringing
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {           

    timeStarted = System.currentTimeMillis();
    String timStarted = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
    callerNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
    TelephonyManager tm = (TelephonyManager) context.getSystemService(
            Context.TELEPHONY_SERVICE);
    isRoaming = tm.isNetworkRoaming();

    // set timeAnswered to -1;
    timeAnswered = -1L;
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");

    Date resultdate = new Date(timeAnswered);
    Log.d("TEST","TimePRatik "+sdf.format(resultdate));
    Log.d("TEST", "timeStarted: " + (timStarted));
    Log.d("TEST", "caller number: " + callerNumber);
    Log.d("TEST", "isRoaming: " + isRoaming);
    return;
}

// call was answered
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK) && timeStarted != -1L) {
    String timAnswered = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
    timeAnswered = System.currentTimeMillis();
    Log.d("TEST", "timeAnswered: " + timAnswered);

    return;
}

// call was ended
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE) && timeStarted != -1L) {
    String timEnded = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
    timeEnded = System.currentTimeMillis();
    Log.d("TEST", "timeEnded: " + timEnded);
    timeStarted = -1L; // DON'T FORGET!
    Intent i = new Intent(context,RecordHistory.class);
    i.putExtra("phonenumber", callerNumber);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Log.d("TEST", "Context of "+context.getClass());
    context.setTheme(android.R.style.Theme_Translucent_NoTitleBar);
    context.startActivity(i); 

    return;
}
}

}

代码片段会更有帮助。是否在Pause()上“注销”广播接收器?使用单独的广播接收器类。同时提供您的代码片段我更新我的问题…@EvZ:不,我不会在onPause中注销我的广播,