0

Wenn ich eine Android-App zu entwickeln. Ich möchte eine Nummer programmatisch anrufen. Bei der VerwendungAndroid Start Aktivität zeigt Fehler

Uri number = Uri.parse("tel:123456789"); 
Intent callIntent = new Intent(Intent.ACTION_DIAL, number); 
getContext().startActivity(callIntent); 

Es zeigt einen Fehler an. Ich verwende FrameLayout anstelle von Aktivität.

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 
at android.app.ContextImpl.startActivity(ContextImpl.java:1366) 
at android.app.ContextImpl.startActivity(ContextImpl.java:1353) 
at android.content.ContextWrapper.startActivity(ContextWrapper.java:322) 
at com.happiness.lockscreenlibrary.view.LockView$1.onDrag(LockView.java:163) 
at android.view.View.dispatchDragEvent(View.java:18339) 
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1492) 
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1478) 
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1478) 
at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:5143) 
at android.view.ViewRootImpl.access$700(ViewRootImpl.java:108) 
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3331) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5312) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 

Ich habe context.getApplicationContext().startActivity(callIntent); oder context.startActivity(callIntent); statt getContext().startActivity(callIntent); Aber es zeigt den gleichen Fehler verwendet. Wie es gelöst wird, bitte hilf mir.

+1

'Aufruf startActivity() von außerhalb eines Aktivitäts Kontext erfordert die FLAG_ACTIVITY_NEW_TASK flag.' –

Antwort

2

Um die Aktivität vom Dienst oder von wo auch immer mit Ausnahme einer anderen Aktivität zu starten, müssen Sie das Flag FLAG_ACTIVITY_NEW_TASK der Absicht hinzufügen.

Uri number = Uri.parse("tel:123456789"); 
Intent callIntent = new Intent(Intent.ACTION_DIAL, number); 
callIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getContext().startActivity(callIntent); 
0

Versuchen Sie, Ihre Absicht, das Hinzufügen Flagge:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

wird Ihr Code

sein
Uri number = Uri.parse("tel:123456789"); 
Intent callIntent = new Intent(Intent.ACTION_DIAL, number); 
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getContext().startActivity(callIntent); 

Hoffnung Ihnen helfen!

2

Sie müssen SetFlags (Intent.FLAG_ACTIVITY_NEW_TASK)

Wenn Satz hinzuzufügen, diese Tätigkeit der Beginn einer neuen Aufgabe auf diesem Geschichte Stapel werden wird.

Uri number = Uri.parse("tel:123456789"); 
Intent callIntent = new Intent(Intent.ACTION_DIAL, number); 
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getContext().startActivity(callIntent); 
1
Uri number = Uri.parse("tel:123456789"); 
Intent callIntent = new Intent(Intent.ACTION_DIAL, number); 
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(callIntent); 
// use getActivity().startActivity(callIntent); if you are inside a fragment 

Sie können hier Informationen über Absicht Flaggen erhalten:

https://developer.android.com/reference/android/content/Intent.html

FLAG_ACTIVITY_NEW_TASK Wenn gesetzt, wird diese Aktivität der Beginn eine neue Aufgabe auf dieser Geschichte werden Stapel.

0

Sie sollten den Kontext aus der Aktivität übergeben, um mithilfe von Konstruktor anzuzeigen.

new LockView(this); 

Verwenden Sie den gleichen Kontext, um eine neue Aktivität zu starten.

Uri number = Uri.parse("tel:123456789"); 
    Intent callIntent = new Intent(Intent.ACTION_DIAL, number); 
    callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(callIntent); 

Fügen Sie Intent.FLAG_ACTIVITY_NEW_TASK hinzu, um eine neue Aktivität zu starten.

1

rufen Sie Ihre startActivity mit FLAG_ACTIVITY_NEW_TASK. logcat zeigt den Fehler und Sie immer noch Entsendung es auf SO

Uri number = Uri.parse("tel:123456789"); 
Intent callIntent = new Intent(Intent.ACTION_DIAL, number); 
callIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getContext().startActivity(callIntent);