2016-01-06 87 views
1

我在我的應用程序中創建了一個通知,但是當我點擊按鈕來顯示它時,從不顯示並且日誌貓沒有顯示任何錯誤。我不知道該做什麼。我的通知不會顯示從不

import android.support.v4.app.NotificationCompat; 

Intent resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://" + fileDir)); 
    PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
        this, 
        0, 
        resultIntent, /* <---- el intent que se realiza al hacer click*/ 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 

    //TODO creamos notificacion 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setWhen(System.currentTimeMillis()) 
        .setTicker("Downloader PRO") 
        .setContentTitle(getText(R.string.notificacion_titulo)) 
        .setContentText("prueba") 
        .setContentIntent(resultPendingIntent) 
        .setContentInfo(getText(R.string.notificacion_informacion)); 

    mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 

    //TODO mostramos la notificacion 

    // Sets an ID for the notification 
    int mNotificationId = 001; 
    // Gets an instance of the NotificationManager service 
    NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    // Builds the notification and issues it. 
    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 

滿級:

package com.toniapps.downloaderpro; 

import android.content.DialogInterface; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Patterns; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Toast; 

import com.loopj.android.http.AsyncHttpClient; 
import com.loopj.android.http.FileAsyncHttpResponseHandler; 

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

import cz.msebera.android.httpclient.Header; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    //recibir información 
    Intent intent = getIntent(); 
    String action = intent.getAction(); 
    String type = intent.getType(); 

    if (Intent.ACTION_SEND.equals(action) && type != null) { 
     if ("text/plain".equals(type)) { 
      handleSendText(intent); // Handle text being sent 
     } else if (type.startsWith("image/")) { 
      handleSendImage(intent); // Handle single image being sent 
     } 
    } 


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      descargarB(view); 
     } 
    }); 
} 

void handleSendText(Intent intent) { 
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); 
    if (sharedText != null) { 
     if(sharedText.contains("Tweet")){ 
      twitter(sharedText); 
     }else{ 
      String enlace = decodificarUrl(sharedText); 
      urlCompartida(enlace); 
      buscar(enlace); 
     } 
    } 
} 

void handleSendImage(Intent intent) { 
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); 
    if (imageUri != null) { 
     try { 
      Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri); 
      guardarImagen(bitmap); 
     } catch (IOException e) { 
      Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    } 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return false; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
    public void buscarB(View view){ 
     EditText urlT = (EditText) findViewById(R.id.url_text_view); 
     String url = urlT.getText().toString(); 

     if (url.contains("instagram")){ 
      instagram(); 
     }else { 
      buscar(url); 
     } 
    } 
    private void buscar(String url){ 
     AsyncHttpClient client = new AsyncHttpClient(); 



     client.get(url, new FileAsyncHttpResponseHandler(/* Context */ this) { 
      @Override 
      public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) { 
       Toast.makeText(MainActivity.this, throwable.getMessage(), Toast.LENGTH_LONG).show(); 
      } 

      @Override 
      public void onSuccess(int statusCode, Header[] headers, File file) { 
       ImageView imagen = (ImageView) findViewById(R.id.imagen); 
       imagen.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath())); 
      } 
     }); 
    } 
    public void descargarB(View view){ 
     EditText urlT = (EditText) findViewById(R.id.url_text_view); 
     String url = urlT.getText().toString(); 

     if (url.contains("instagram")){ 
      instagram(); 
     }else { 
      descargar(url); 
     } 


    } 

private void descargar(String url) { 
    AsyncHttpClient client = new AsyncHttpClient(); 


    client.get(url, new FileAsyncHttpResponseHandler(/* Context */ this) { 
     @Override 
     public void onFailure(int i, Header[] headers, Throwable throwable, File file) { 
      Toast.makeText(MainActivity.this, throwable.getMessage(), Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onSuccess(int i, Header[] headers, File file) { 

      ImageView imagen = (ImageView) findViewById(R.id.imagen); 
      imagen.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath())); 

      Bitmap imagenSC = BitmapFactory.decodeFile(file.getAbsolutePath()); 

      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      imagenSC.compress(Bitmap.CompressFormat.PNG, 100, bytes); 
      File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/Downloader_PRO/"); 
      path.mkdirs(); 
      File imageFile = new File(path, (file.getName() + ".png")); 
      try { 
       imageFile.createNewFile(); 
       FileOutputStream fo = new FileOutputStream(imageFile); 
       fo.write(bytes.toByteArray()); 
       fo.flush(); 
       fo.close(); 
       Toast.makeText(MainActivity.this, imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 
    } 
    private void guardarImagen(Bitmap bitmap) { 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes); 
     File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/Downloader_PRO/"); 
     path.mkdirs(); 
     File imageFile = new File(path, (bitmap.getByteCount()+".png")); 
     try { 
      imageFile.createNewFile(); 
      FileOutputStream fo = new FileOutputStream(imageFile); 
      fo.write(bytes.toByteArray()); 
      fo.flush(); 
      fo.close(); 
      crearNotificacionDescarga(imageFile.getAbsolutePath()); 
      Toast.makeText(MainActivity.this, imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    } 
    private void urlCompartida(String url){ 
     EditText urlT = (EditText) findViewById(R.id.url_text_view); 
     urlT.setText(url); 
    } 
    private String decodificarUrl(String urlBase){ 

     ArrayList links = new ArrayList(); 
     String urlStr =""; 
     Pattern p = Patterns.WEB_URL; 
     Matcher m = p.matcher(urlBase); 
     while(m.find()) { 
      urlStr = m.group(); 
      if (urlStr.startsWith("(") && urlStr.endsWith(")")) { 
       urlStr = urlStr.substring(1, urlStr.length() - 1); 
      } 
      links.add(urlStr); 
     } 
     return urlStr; 
    } 
    private void twitter(final String tweetText){ 
     new AlertDialog.Builder(this) 
       .setTitle(getText(R.string.Twitter_titulo)) 
       .setMessage(getText(R.string.Twitter_mensaje_uno) + "\n" + "\n" + getText(R.string.Twitter_mensaje_dos)) 
       .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         String enlace = decodificarUrl(tweetText); 
         Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(enlace)); 
         startActivity(browserIntent); 
         dialog.dismiss(); 
        } 
       }) 
       .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // do nothing 
        } 
       }) 
       .setIcon(R.drawable.ic_warning_black_48dp) 
       .show(); 
    } 
private void instagram(){ 
    new AlertDialog.Builder(this) 
      .setTitle(getText(R.string.instagram_titulo)) 
      .setMessage(getText(R.string.instagram_mensaje_1) + "\n" + "\n" + getText(R.string.instagram_mensaje_2)) 
      .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 

       } 
      }) 
      .setIcon(R.drawable.ic_warning_black_48dp) 
      .show(); 
} 
private void crearNotificacionDescarga(String fileDir){ 

    new Intent(Intent.ACTION_VIEW, Uri.parse("content://" + fileDir)); 


} 
} 

IDK現在做什麼,在任何設備工程....

+0

確保您的應用程序的信息已通知檢查 –

+0

我如何檢查呢?? – Toni

回答

0

將行:

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

有:

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

在設置中選擇應用程序,選擇您感興趣的應用程序並設置顯示通知複選框。

enter image description here

+0

我嘗試了2個手機,它不工作... – Toni

+0

我更新了簡單的替換你的代碼,檢查它@Toni –

+0

是的是的,但它無法正常工作uu – Toni