Intent myIntentA = new Intent(MainMap.this, AudioStream.class);
myIntentA.putExtra(AUDIO_STREAM,AUDIO_STREAM_URL);
MojProg.this.startActivity(myIntentA);
这行不通。因为“this”的意思是“这个类”。您不能在其他课程上使用它(是的,您可以,但有不同的方法。请在论坛、本网站或 oracle 网站上研究“this”。)。这就是那个警告的原因。
好吧,看起来您的问题是“如何将上下文拉到非活动类?”。(Intent() 的第一个参数是 Context)。
为此,您可以在 Main Activity 中创建一个 Context 瞬间并将您的基本上下文分配给它,例如:
static Context context;
....
context = this.getBaseContext();
不要忘记那是你的主要活动。然后在你的非活动类中,你可以提取这个上下文并使用它,比如:
Context context;
Intent intent;
....Constructor:
context = MainActivity.context;
intent = new Intent(context, YourSecondActivity.class); // you have to declare your second activity in the AndroidManifest.xml
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this is required to call "Intent()" in a non-activity class.
//And then you can call the method anywhere you like (in this class of course)
context.startActivity(intent);
好的。您已准备好再迈出一步。在 AndroidManifest.xml 中,像第一个一样声明您的第二个活动;
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".YourSecondActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
</activity>
你现在准备好了。但是最后一个警告,不要忘记在打开另一个活动之前处理您的活动以避免滞后。玩得开心。