0

So cant seem to get this configured or running properly.

Trying to get the notification to open the MainActivity3 file and then to have it run whatever I want, at the minute just aiming for it to play a sound.

Completely new to android development but think Im along the right lines. You will see I have two approaches but cant get either to work.

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;
}



}

Once the notification is called and appears its meant to run this file 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;


}
}

The error seems to be that the call is static but it wants void, very confused Error code

Cannot make a static reference to the non-static method onNewIntent() from the type MainActivity3 MainActivity.java /firstApp/src/com/example/firstapp line 38

and if I change the method to fit then the mediaplayer getApplicationContext wont work

after doing more research I feel the manifest might be stopping it but still unsure to the structure of android.

Manifest

 <?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>

Please help as getting really boged down and tempting to give up

PKYNI
  • 41
  • 9
  • Are you aware that it should call the acitivity3 when you click on notification, not when it appears? And for Notifications it is better use getApplicationContext, not getBaseContext. – Yaroslav Mytkalyk Feb 12 '13 at 17:51
  • yes i want it to call it when the notification is clicked, il try changing to getApplicationContext but not sure I understand what the difference is. basically when click the notification want it to run a function from MainActivity3 – PKYNI Feb 12 '13 at 18:11

1 Answers1

0

You are not doing anything with PendingIntent. You should set it in Notification Builder before calling build()

mBuilder.setContentIntent(pendingIntent);

Also you must be aware that creating a new Activity and doing something on onCreate is not a good idea for executing an operation. If you want, for example to show a toast in the same activity you can set specific data in Intent and do it in onNewIntent(). The accepted answer has an example for configuring handling of new intent.

Android: new Intent() starts new instance with android:launchMode="singleTop"

Community
  • 1
  • 1
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
  • ok hasnt really helped, i think it is now picking up the correct file and the correct method/function but there is a problem with their declarations. currently the notification constructor in MainActivity is – PKYNI Feb 13 '13 at 13:10