0

I need to start an app when the [Android] phone starts.
I compiled this code, and the app doesn't crash but doesn't show me anything either!
Now I'm trying with Toast but it still isn't found. Can someone help me?

This is the main activity:

package com.example.simplenotification;

import android.app.Activity;
import android.app.AlarmManager;
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.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;  
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MainActivity extends Activity {

 private static final int MY_NOTIFICATION_ID = 0;

 private int mNotificationCount;

 private final CharSequence tickerText ="this is tickerText";
 private final CharSequence contentTitle="this contentTitle";
 private final CharSequence contentText="this coontentText";

 private Intent mNotificationIntent;
 private PendingIntent mContentIntent;

 RemoteViews mContentView = new RemoteViews("com.example.simplenotification.StatusBarWithCustomView", R.layout.custom_view);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNotificationIntent = new Intent(getApplicationContext(),AppGet.class);
    mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0, mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    final Button button = (Button) findViewById(R.id.button1);


    button.setOnClickListener(new OnClickListener() {


        public void onClick(View v){


            startNotification();


        }



    });


 }


public void startNotification(){
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
    .setTicker(tickerText)
    .setSmallIcon(R.drawable.ic_launcher)
    .setAutoCancel(true)
    .setContentTitle("titolo content")
    .setContentText("content text")
    .setContentIntent(intent);


    NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(getApplicationContext());
    mNotificationManager.notify(MY_NOTIFICATION_ID,notificationBuilder.build());
}
}

This the Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simplenotification"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="14" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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>

I don't know how debug the app autorun on emulator. I'm sorry

EDIT: I put in another class the reciever and changed the 'android name'

    package com.example.simplenotification;

    import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BootUpReceiver extends BroadcastReceiver {
    @Override
        public void onReceive(Context context, Intent intent) 
    {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent i = new Intent();
            i.setAction("android.intent.action.MAIN");
            context.startService(i);
            Toast.makeText(context , "saranno 3?" , Toast.LENGTH_SHORT).show();

        }

    }

}
Gabrio
  • 388
  • 1
  • 4
  • 17
  • Too much code! Narrow it down a bit, and while you're at it, what's the error you're getting? – Cullub Sep 20 '14 at 12:40
  • make your Receiver class as a separate class not as inner. – kalyan pvs Sep 20 '14 at 12:43
  • Yup and it looks vaguely correctly, you need an `intent-filter` and a `BroadcastReceiver`, which you've got. I'd advise against debugging this on an emulator, frankly I don't even know if there's a way to do this (there ought to be though). – Aleksander Lidtke Sep 20 '14 at 12:43
  • Your code is a little mangled. Is the BroadcastReceiver an inner class of MainActivity? Also, the permission is incorrect. – Mike M. Sep 20 '14 at 14:13
  • 1
    [This Answer](http://stackoverflow.com/a/19856267/3989632) should pretty much get you covered... – nadavfima Sep 20 '14 at 14:18
  • @MikeM. no, the receiver isn't an inner class of MainActivity, why the permission is incorrect? – Gabrio Sep 20 '14 at 14:41
  • @NadavFima you ar saying that i can't show a toast or start a background process in autorun mode? – Gabrio Sep 20 '14 at 14:43
  • The app needs the permission. The `permission` attribute on the Receiver does not provide that. Look at the answer in the link Nadav provided. – Mike M. Sep 20 '14 at 14:53

2 Answers2

0

Make a separate class for BootUpReceiver. And in manifest pass correct path of ur package name in android:name while defining receiver

 <receiver android:name=".receivers.BootUpReceiver"

Update->

Also use this permission in your manisfest

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Avinash Kumar Pankaj
  • 1,700
  • 2
  • 18
  • 27
0

Not 100% sure, but try changing this

Toast.makeText(context , "saranno 3?" , Toast.LENGTH_SHORT).show();

into

Toast.makeText(context.getApplicationContext() , "saranno 3?" , Toast.LENGTH_SHORT).show();
nadavfima
  • 3,012
  • 1
  • 11
  • 9