所以似乎无法正确配置或运行它。
试图获得打开 MainActivity3 文件的通知,然后让它运行我想要的任何东西,此刻只是为了让它播放声音。
对 android 开发完全陌生,但认为我是正确的。您会看到我有两种方法,但都无法正常工作。
MainActivity.java
package com.example.firstapp;
import android.media.MediaPlayer;
//import android.media.RingtoneManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
//import android.app.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//vibrate controls
//Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.follownavi)
.setContentTitle("Hey")
.setContentText("");
PendingIntent pendingIntent;
//Intent intent = new Intent();
Intent intent= new Intent(MainActivity3.onNewIntent(), MainActivity3.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//intent.setClass(getApplicationContext(),MainActivity3.class);
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
mBuilder.addAction(R.drawable.follownavi,"LISTEN",pendingIntent);
mBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//get touch location
int x = (int)event.getX();
int y = (int)event.getY();
//put on screen
TextView tester = (TextView) findViewById(R.id.textView1);
tester.setText("this is x:" + x + " and this is y:" + y);
//move image
ImageView iv = (ImageView) findViewById(R.id.NaviFollow);
if(y > 222)
{
iv.setX(x - 125);
iv.setY(y - 350);
//cause vibration
Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
v.vibrate(300);
}
return true;
}
}
一旦通知被调用并出现它意味着运行这个文件 MainActivity3
package com.example.firstapp;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
public class MainActivity3 extends Activity {
protected Context onNewIntent()
{
MediaPlayer mp2 = MediaPlayer.create(getApplicationContext(), R.raw.listen);
mp2.start();
return null;
}
}
错误似乎是调用是静态的,但它想要无效,非常混乱错误代码
无法从类型 MainActivity3 MainActivity.java /firstApp/src/com/example/firstapp 第 38 行对非静态方法 onNewIntent() 进行静态引用
如果我更改方法以适应,那么媒体播放器 getApplicationContext 将无法工作
在做了更多研究之后,我觉得清单可能会阻止它,但仍然不确定 android 的结构。
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/navibutton"
android:label="Navi"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.firstapp.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:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
请帮助,因为真的陷入困境并很想放弃